1

I am a beginner at JavaScript so I'm sorry if this is a noob question. I'm trying to use XmlHttpRequest to call an HTML file (example.html) with a JavaScript (such as an RSS feed script) in the called HTML file. Any scripts in the called HTML file (example.html) won't load. It will load any plain text or hyperlinks in the called HTML file though. I've even tried changing the called file. For example switch "example.html" with "rssfeeder.js" to see if it'd load the JavaScript on demand, but that still didn't work. Again, I'm very new at JavaScript, so please be detailed in your answers. Thanks! Here is the script I'm using:

           var receiveReq = getXmlHttpRequestObject();      
           function Example() {
               if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
                   receiveReq.open("GET", 'example.html', true);
                   receiveReq.onreadystatechange = handleExample; 
                   receiveReq.send(null);
               }            
       }
          function handleExample() {
              if (receiveReq.readyState == 4) {
           document.getElementById('span_result').innerHTML = receiveReq.responseText;
               }
          }
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • This has nothing to do with Java. Please see [What's the difference between JavaScript and Java? ](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java). – Matthew Flaschen Dec 29 '10 at 05:27

1 Answers1

2

The browser will not automatically run the results of an XHR result. If you know you are getting javascript as a result and want that to run automatically try including it as a script element instead:

document.body.appendChild(document.createElement('script')).src="example.js";

If the page actually is html with some javascript functions within then call the function by name after you set the innerHTML.

If the scripts are anonymous script blocks without any functions defined then you can try to do something like:

var myscripts = document.getElementById('span_result').getElementsByTagName('script');
for (var i in myscripts) {
  eval(myscripts[i].innerHTML);
}
Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
  • adding to above : you could have anonymous self executing functions to trigger the javascript right ? something like : function(){ // blah blah } – Shrinath Dec 29 '10 at 06:02