By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,695 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,695 IT Pros & Developers. It's quick & easy.

Execute A page contain javascript in AJAX (innerHTML Property)

P: 1
Salam
I have a Problem to load my optional page in AJAX.
I send command to innerHTML
result = http.responseText;
document.getElementById('MyAjax').innerHTML = result;
My result variable contain script tag. But No action is appeard.
Help Me Please.
Thanks at all.
Dec 3 '06 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
To execute JavaScript returned via Ajax, either eval it or add it to the page using dynamic script tags, e.g.
Expand|Select|Wrap|Line Numbers
  1. var script = document.createElement("script");
  2. script.type = "text/javascript";
  3. script.appendChild(document.createTextNode(txt));//txt is the code
  4. var head = document.getElementsByTagName("head")[0];
  5. head.appendChild(script);
May 31 '08 #2

P: 5
I think I'm having the same issue, and am not sure where to integrate your code snippet into my code to test it.

To clarify my situation, a div is populated with a page, say 'text.html' using innerHTML. Any scripts within text.html do not execute while in the div, but when it is loaded directly, the scripts execute perfectly.

Do you have a larger sample of that code?
Jul 25 '08 #3

rnd me
Expert 100+
P: 427
if the response contains only javascript, eval the responseText.

if it contains mixed html w/ script tags, IE will not execute the javascript.

you will need to use a regexp to find the code inside the script tag, and eval that after injecting the innerHTML.
Jul 26 '08 #4

P: 5
if the response contains only javascript, eval the responseText.

if it contains mixed html w/ script tags, IE will not execute the javascript.

you will need to use a regexp to find the code inside the script tag, and eval that after injecting the innerHTML.
Tried that. No change.

Something about my code may be the cause.

This is the code I use to call the inner.HTML
Expand|Select|Wrap|Line Numbers
  1. function xhrRequest(type) {
  2.     var xhrSend;
  3.     if (!type) {
  4.         type = 'text';
  5.     }
  6.     if (window.ActiveXObject) {
  7.         try {
  8.             xhrSend = new ActiveXObject("Msxml2.XMLHTTP");
  9.         } catch (e) {
  10.             try {
  11.                 xhrSend = new ActiveXObject("Microsoft.XMLHTTP");
  12.             } catch (e) {}
  13.         }
  14.     } else if (window.XMLHttpRequest) {
  15.         xhrSend = new XMLHttpRequest();
  16.         if (xhrSend.overrideMimeType) {
  17.             xhrSend.overrideMimeType('text/' + type);
  18.         }
  19.     }
  20.     //document.getElementById('area1').innerHTML += new Date().getTime() + ' Sending xhr:' + xhrSend + '<br />';
  21.     return (xhrSend);
  22. }
  23.  
  24. function whatWhere(url, boxid) {
  25.     var    reqType = "text";
  26.     //    alert("WhatWhere");
  27.     var xhrRec = xhrRequest(reqType);
  28.  
  29.  
  30.  
  31.         document.getElementById(boxid).innerHTML = "**";
  32.         xhrRec.open('GET', url, true);
  33.         xhrRec.onreadystatechange = function() {
  34.             //document.getElementById('testlog').innerHTML += new Date().getTime() + ' Requesting ' + url + ' ' + xhrRec.readyState + '-' + xhrRec.status + '<br />';
  35.             if (xhrRec.readyState == 4 && xhrRec.status == 200) {
  36.                 //document.getElementById('testlog').innerHTML += new Date().getTime() + ' Received ' + url + '<br />';
  37.                 document.getElementById(boxid).innerHTML = xhrRec.responseText;
  38.                 //document.getElementById('filename').innerHTML = url;
  39.                 xhrRec = null;
  40.             } else if (xhrRec.readyState == 4 && xhrRec.status == 404) {
  41.                 document.getElementById(boxid).innerHTML = xhrRec.statusText;
  42.                 //document.getElementById('filename').innerHTML = url;
  43.                 xhrRec = null;
  44.             }
  45.         };
  46.         xhrRec.send(null);
  47. }
  48.  
And this is the code on the page that gets called in the div
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. alert("Hello World. The Script Executed");
  3. </script>
  4.  
Jul 26 '08 #5

rnd me
Expert 100+
P: 427
Expand|Select|Wrap|Line Numbers
  1.  
  2.     function xhrRequest(type) {
  3.             type = type ||  "html";
  4.             xhrSend =  !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
  5.             if (xhrSend.overrideMimeType) {   xhrSend.overrideMimeType("text/" + type); }
  6.         return xhrSend;
  7.     }
  8.  
  9.  
  10. function whatWhere(url, boxid) {
  11.    var reqType = "text";
  12.    var xhrRec = xhrRequest(reqType);
  13.    document.getElementById(boxid).innerHTML = "**";
  14.    xhrRec.open("GET", url, true);
  15.    xhrRec.onreadystatechange =    function () {
  16.         if (xhrRec.readyState == 4 && xhrRec.status == 200) {
  17.         var rt= xhrRec.responseText;
  18.                document.getElementById(boxid).innerHTML = rt;
  19.         var js = tr.replace(/\<script[\w\W]+?\>/i,"").replace(/\<\\script\s*\>/i,"");
  20.         try { eval(js); } catch(err){  document.getElementById(boxid).innerHTML = String(err); }
  21.                xhrRec = null;        
  22.     }
  23.        if (xhrRec.readyState == 4 && xhrRec.status == 404) {
  24.             document.getElementById(boxid).innerHTML = xhrRec.statusText;
  25.             xhrRec = null;                
  26.     }
  27.    }
  28.         xhrRec.send(null);
  29.  }
  30.  
  31.  
Jul 26 '08 #6

P: 5
Thanks for the tips. That's much cleaner.

However, was this a typo rt.replace. Did you intend str.replace or perhaps rt.replace?

I've tried both. str.replace has no effect on the code, and my tweaks didn't have any success. rt.replace throws all kinds of errors.



Expand|Select|Wrap|Line Numbers
  1. var rt= xhrRec.responseText;
  2.                document.getElementById(boxid).innerHTML = rt;
  3.         var js = tr.replace(/\<script[\w\W]+?\>/i,"").replace(/\<\\script\s*\>/i,"");
  4.  
Jul 26 '08 #7

rnd me
Expert 100+
P: 427
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. var js = rt.replace(/\<script[\w\W]+?\>/i,"").replace(/\<\\script\s*\>/i,"");
  4.  
  5.  
Jul 26 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.