Skip to content Skip to sidebar Skip to footer

Trying To Call Js Code That Is Passed Back From Ajax Call

Okay, so I have an javascript function that retrieves some HTML... function updateQuestions(i){ var url = 'getQuestions.php?sys=' + i; if (receiveReq.readyState == 4 || rec

Solution 1:

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

Solution 2:

My answer from How To Call Javascript In Ajax Response? IE: Close a form div upon success

// response is the data returned from the servervar response = "html\<script type=\"text/javascript\">alert(\"foo\");<\/script>html";
var reScript = /\<script.*?>(.*)<\/script>/mg;
response = response.replace(reScript, function(m,m1) {
    varfn = newFunction(m1); // this will make the eval run in the global scopefn(); //will run alert("foo");return"";
});
alert(response); // will alert "htmlhtml"

Solution 3:

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 codeHoldervar javascriptCode="function test(){.....}";
varJSONCode=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);

Solution 4:

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.

Solution 5:

There is also responseXML:

receiveReq.responseXML.getElementsByTagName('input')

etc., etc.

Post a Comment for "Trying To Call Js Code That Is Passed Back From Ajax Call"