1

I'm Building a Personal Assistant with HTML5, CSS, & Javascript

This is what my current Problem is.

I Have a <div> that contains scripts with the responses for when a Question is asked and if the question matches one it writes that response.

I Need a Script that will check if this div is empty, in Javascript, not jQuery

I need to check if the div is empty because that means there is no response.

Then for it to redirect to an the error page.

a Question & the response to it would look like this:

    <script type="text/javascript">
{
var q = getValue("q");
var name = getValue("name");
}
if (q == 'Whats+up') {
document.write("Not much " + name + ".");
}
else {

}
</script>

This is the last code I tried:

            <script>

    var theDiv = document.getElementById("responce");
    var q = getValue("q");
    if(theDiv.innerHTML == ''){
    window.location = 'error.html?name=Brandon&q='+q;
    }
  </script>
Sam
  • 7,252
  • 16
  • 46
  • 65
electrikmilk
  • 1,013
  • 1
  • 11
  • 23
  • 1
    Just to be sure : the div is filled by your own server ? [If not, you can't look at what is inside the div](http://en.wikipedia.org/wiki/Same_origin_policy). – Denys Séguret Oct 12 '12 at 17:06
  • "I Have a
    that contains scripts..." If this is true, `innerHTML`of the `DIV` is never `''`, though you can't see anything in the browser, the script is a part of the HTML of the `DIV`.
    – Teemu Oct 12 '12 at 17:28
  • if no one knows how to fix this is there _another way_ this could work? – electrikmilk Oct 12 '12 at 18:06
  • I tried having one of the question & response scripts `document.write("error")` and have a script check if that's the only word in the div but it never worked out, any thoughts? – electrikmilk Oct 17 '12 at 15:45
  • Be careful with `document.write`. It's purposed to create the whole new document rather than append text in the present document, i.e. if it is used from a function after the page has been parsed, it automatically calls `document.open()`, which wipes out ALL earlier code in the document. – Teemu Oct 17 '12 at 16:58
  • its done that on most of my documents but with this it just writes the specified code where I want. – electrikmilk Oct 17 '12 at 17:11

2 Answers2

1

Try this

<script type="text/javascript">
{
var q = getValue("q");
var name = getValue("name");
}
if (q == 'Whats+up') {
document.write("<div id='response_text'>"+"Not much " + name + "."+"</div>");
}
else {

}
</script>

And then:

<script>

var theDiv = document.getElementById("response_text");
var q = getValue("q");
if(theDiv==null || theDiv.innerHTML == ''){
window.location = 'error.html?name=Brandon&q='+q;
}
</script>

That being said, there are probably better/more scalable ways of doing this with ajax.

Pyro979
  • 202
  • 1
  • 10
  • I tried that and the error script made it so every question went to the error page. I tried messing around with it to make it work, but thank you for the help! – electrikmilk Oct 12 '12 at 18:08
  • if no one knows how to fix this is there another way this could work? – electrikmilk Oct 12 '12 at 18:09
  • I'd have to see the code to try to fix it. is there anyway you can set up a http://jsfiddle.net/? – Pyro979 Oct 12 '12 at 19:49
  • **beware**, it must have a `?q=some%20query` to generate a responce – electrikmilk Oct 15 '12 at 12:47
  • I tried having one of the question & response scripts `document.write("error")` and have a script check if that's the only word in the div but it never worked out, any thoughts? – electrikmilk Oct 17 '12 at 15:45
1

You could check, if there are no other elements than scripts in the div:

<script>
    var theDiv = document.getElementById('responce'),
        q = getValue('q'),
        scripts = theDiv.getElementsByTagName('SCRIPT').length;
    if (theDiv.children.length - scripts === 0) {
        window.location = 'error.html?name=Brandon&q=' + q;
    }
</script>

However, I'd suggested you to get rid of all inline scripts, and put them in the head of the document or just before the closing body tag instead.

EDIT

A quick-fix would be to move the script below the #responce.

Teemu
  • 22,918
  • 7
  • 53
  • 106
  • I tried this and I'm not sure, your script says to have the scripts in the div, but you're saying to have them out. I tried this with the scripts in and out and both didn't work, but thank you for your help! – electrikmilk Oct 15 '12 at 13:37
  • I tried having one of the question & response scripts `document.write("error")` and have a script check if that's the only word in the div but it never worked out, any thoughts? – electrikmilk Oct 17 '12 at 15:44