2

How to make javascript code execute on php page which was previously loaded from an ajax call?

code samples: ajax+function parseScript to force javascript to run on the ajax requested page

Senario: I am selecting a question from selectQuest.php and using ajax it request a page called showRecord.php.

The page showRecord.php will display a table which contain the information corresponding to the quetsion selected.It contains the javascript that do not run.The javascript return will allow me to update info in db when i click submit.

The code sample below is found in the showRecord.php.Finally, if the latter run the showRecord will make another ajax request for updateQuestion.php.

But the javascript is not running in showRecord.php

  function show(fileTex,aTex,bTex,cTex,dTex,eTex,newQuestNumTex,textAreaTex,textQuesAnsTex)
    {

  if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
   else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4)
    {
    var resp=xmlhttp.responseText;
    document.getElementById("txtHint2").innerHTML=resp;
    parseScript(resp);
  // document.getElementById("txtHint").style.border="1px solid #A5ACB2";
    }
  }
xmlhttp.open("POST","updateQuestionAdmin.php?param="+fileTex+"&text_A="+aTex+"&text_B="+bTex+"&text_C="+cTex+"&text_D="+dTex+"&text_E="+eTex+"&text_ID="+newQuestNumTex+"&text_Ques="+textAreaTex+"&text_Ans="+textQuesAnsTex,true);
xmlhttp.send();
}

// this function create an Array that contains the JS code of every <script> tag in parameter
// then apply the eval() to execute the code in every script collected
function parseScript(strcode) {
  var scripts = new Array();         // Array which will store the script's code

  // Strip out tags
  while(strcode.indexOf("<script") > -1 || strcode.indexOf("</script") > -1) {
    var s = strcode.indexOf("<script");
    var s_e = strcode.indexOf(">", s);
    var e = strcode.indexOf("</script", s);
    var e_e = strcode.indexOf(">", e);

    // Add to scripts array
    scripts.push(strcode.substring(s_e+1, e));
    // Strip from strcode
    strcode = strcode.substring(0, s) + strcode.substring(e_e+1);
  }

  // Loop through every script collected and eval it
  for(var i=0; i<scripts.length; i++) {
    try {
      eval(scripts[i]);
    }
    catch(ex) {
      // do what you want here when a script fails
    }
  }
}
</script>  
DoctorAV
  • 1,189
  • 1
  • 14
  • 40
  • 2
    I would strongly recommend using a Javascript framework such as jQuery or Dojo to make AJAX requests; it'd be a hell of a lot better than what you have here. – q3d Nov 18 '12 at 10:00
  • iyrag any idea how to force javascript to execute on an ajax requested page??? – ted pudmanabadoo Nov 18 '12 at 10:07
  • What script is being returned from `updateQuestionAdmin.php`? Did you try stepping through the JavaScript code with the debugging tools of your browser? Does it reach the `eval` statement with the script you expect to be there? – Frank van Puffelen Nov 18 '12 at 11:56
  • Use some wellknown method to inject (and execute) java scripts - for example, [here](http://stackoverflow.com/a/2343546/1102014). **Do not parse scripts manually and do not eval them**. – Stan Nov 18 '12 at 13:47

1 Answers1

0

As iyrag said, a Javascript framework would help. jQuery has a callback function that you can run when a script is successfully loaded and finished with ajax.

You'll want to execute some other stuff within that callback function, for instance :

$.ajax({
  url: 'test.php',
  success: function(data) {
    $('#result').html(data); // Display script return on some div
    someFunction(); // blabla

// Execute some other javascript here, you'll be abble to access the DOM of the test.html
// page because here you'lle be sure that test.html is loaded.showRecord.php should not contain javascript, which would rather be here

  }
});

I also posted this because the tag features jQuery...

Romain Bruckert
  • 2,546
  • 31
  • 50