469,325 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,325 developers. It's quick & easy.

How to Load a page with javascript into a div problem

I am currently working on a website project. The sites are loaded into a <div> by using ajax. Everything was working well until I tried to load a page into a <div> with some sort of javascript code on that page. The javascript didn't run after that site had been loaded into that <div>.
For example This is the <div> tag on the main page:
Expand|Select|Wrap|Line Numbers
  1.  <div id="loadPage"> </div>
I tried to load another page into that div:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <script type="javascript/text"> 
  4. alert('TEST'); document.write('Doesn't work');</script>
  5. </body>
  6. </html>
None of those javascript lines were executive.

Pretty easy. I used DOM method to detach the body part.
Expand|Select|Wrap|Line Numbers
  1. function makeAjaxObject()
  2. {    
  3.     //For older broswsers and IE
  4.     var xmlObj = false;
  6.     try
  7.     {
  8.         xmlObj = new ActiveXObject("Msxml2.XMLHTTP");
  9.     }
  10.     catch(e)
  11.     {
  12.         try
  13.         {
  14.             xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  15.         }
  16.         catch(e2)
  17.         {
  18.             xmlObj = false;
  19.         }
  20.     }
  21.     //For browers that support XMLHTTP
  22.     if (!xmlObj && typeof XMLHttpRequest != 'undefined')
  23.       {
  24.           xmlObj = new XMLHttpRequest();
  25.       }
  27.     return xmlObj;
  28. }        
And then make an object and
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(pageLoaderDivID).innerHTML = getBody(loadedContent);            
May 28 '10 #1
7 1818
607 512MB
Can you please post the code how you are loading that page

Thanks and Regards
Ramanan Kalirajan
May 28 '10 #2
Any help please? Has anyone tried to load a page with javascript on it to a <div> and that javascript code still worked?
May 28 '10 #3
8,651 Expert Mod 8TB
javascript code that is loaded through AJAX is not parsed by the JavaScript engine. you have to eval() it or something like that to make use of it.
May 28 '10 #4
Thank you Dormilich. I will find out the other way then
May 29 '10 #5
607 512MB
Can I know why you are using document.write. Because whatever the value you are having in the body you are going to place inside a div. document.write is going to write the value on load of the page. Instead of that you can directly give the value. its my suggestion.

Thanks and Regards
Ramanan Kalirajan
May 30 '10 #6
That was just an example that I wrote. I didn't copy my code because I couldn't access it at that time. :)
May 31 '10 #7
16,027 Expert Mod 8TB
Besides using eval, the other options are to already have the code included on the page (I realise that may not always be possible or feasible or efficient), or include the JavaScript source dynamically to the head:
Expand|Select|Wrap|Line Numbers
  1. var script = document.createElement("script");
  2. // set src and type
  3. script.src = ...
  4. head.appendChild(script);
Jun 2 '10 #8

Post your reply

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

Similar topics

2 posts views Thread by Anshul Seth | last post: by
3 posts views Thread by Francois | last post: by
4 posts views Thread by Lloyd Dupont | last post: by
reply views Thread by tony | last post: by
2 posts views Thread by darren | last post: by
2 posts views Thread by shekharanjali | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.