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;
               }
          }
1

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

adding to above : you could have anonymous self executing functions to trigger the javascript right ? something like : function(){ // blah blah }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.