views:

53

answers:

5

Okay, so I have an javascript function that retrieves some HTML...

function updateQuestions(i){
    var url = 'getQuestions.php?sys=' + i;
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
        receiveReq.open("GET", url, true);
        receiveReq.onreadystatechange = handleQuestionsUpdate; 
        receiveReq.send(null);
    }
}

function handleQuestionsUpdate() {
    if (receiveReq.readyState == 4) {
        var a=receiveReq.responseText;
        document.getElementById('questions').innerHTML=a;
        checkSpeakers(); //Error Occurs Here, even though checkSpeakers() is a function in the returned HTML chunk.
    }   
}

This HTML is not just HTML, but it is more specifically a form and a chunk of javascript. The javascript is hard-coded into the HTML and not referenced by <script src="..">

Is it normal that this retrieved JS code isn't recognized upon call-time? If so, what is my alternative if I need the JS to change every time the div is update?

This is the text being returned to the javascript function.

function checkPillowSpeakers()
{
    var pillowSpeakers = document.getElementById('Q9').value + document.getElementById('Q10').value;
    document.getElementById('PS1').style.display = ((pillowSpeakers > 0)? '' : 'none');
    document.getElementById('PS2').style.display = ((pillowSpeakers > 0)? '' : 'none');
}~ARRAYSEPERATOR~<html>....</html>

The JS Code is seperated from the HTML code by an ~ARRAYSEPERATOR~ tag. The issue is that I don't want to EXECUTE the code at this time, I just want it queued so I can call it on command.

A: 

you should realy think of using an js-lib for ajax for browser-compatibilty and less memory leaks - but if you want to do this by yourself, you have to eval() the passed back javascript before you can use it.

oezi
+1  A: 

You should first try to get the JavaScript part from the HTML content. Then you can easily execute it using eval() function from JavaScript;

streetparade
I don't want to execute it at the time of retrieval. I just want it available for when an input value is changed - the value selected there will change the visibility of other elements.
Dutchie432
+1  A: 

My answer from http://stackoverflow.com/questions/2991295/how-to-call-javascript-in-ajax-response-ie-close-a-form-div-upon-success/2991489#2991489

// response is the data returned from the server
var response = "html\<script type=\"text/javascript\">alert(\"foo\");<\/script>html";
var reScript = /\<script.*?>(.*)<\/script>/mg;
response = response.replace(reScript, function(m,m1) {
    var fn = new Function(m1); // this will make the eval run in the global scope
    fn(); //will run alert("foo");
    return "";
});
alert(response); // will alert "htmlhtml"
Sean Kinsey
The thing is, When I get the form, I want to download a BUNCH of JS functions with it. I don't really want to call them when they are retrieved, but perhaps by pressing a button on the form.
Dutchie432
If the code is returned as DOM0 event handlers (attributes on the elements) then everything should work. Using .innerHTML to set the content should leave these functioning.
Sean Kinsey
Either way, my the updated code will evaluate all `<script>` elements so that the contained code will be available in the global scope. This is according to your question.
Sean Kinsey
A: 

there is also responseXML

receiveReq.responseXML.getElementsByTagName('input')

etc etc

Sonic Soul
A: 

After doing a whole lot of research, it seems Eval() has some memory issues... so I actually came across this solution:

//Firstly, create a div called codeHolder

var javascriptCode="function test(){.....}";
var JSONCode=document.createElement("script");
JSONCode.setAttribute("type","text/javascript");
JSONCode.text=javascriptCode;

var cell=document.getElementById("codeHolder");
if ( cell.hasChildNodes() )
    while ( cell.childNodes.length >= 1 )
        cell.removeChild( cell.firstChild );

cell.appendChild(JSONCode);
Dutchie432