How to Load a page with javascript into a div problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meodanang
    New Member
    • May 2010
    • 6

    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:
    Code:
     <div id="loadPage"> </div>
    I tried to load another page into that div:
    Code:
    <html>
    <body>
    <script type="javascript/text"> 
    alert('TEST'); document.write('Doesn't work');</script>
    </body>
    </html>
    None of those javascript lines were executive.

    Pretty easy. I used DOM method to detach the body part.
    Code:
    function makeAjaxObject()
    {	
    	//For older broswsers and IE
    	var xmlObj = false;
    
    	try
    	{
    		xmlObj = new ActiveXObject("Msxml2.XMLHTTP");
    	}
    	catch(e)
    	{
    		try
    		{
    			xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		catch(e2)
    		{
    			xmlObj = false;
    		}
    	}
    	//For browers that support XMLHTTP
    	if (!xmlObj && typeof XMLHttpRequest != 'undefined')
      	{
      		xmlObj = new XMLHttpRequest();
      	}
    	
    	return xmlObj;
    }
    And then make an object and
    Code:
    document.getElementById(pageLoaderDivID).innerHTML = getBody(loadedContent);
    Last edited by Niheel; May 28 '10, 05:55 PM. Reason: merged question details
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Can you please post the code how you are loading that page

    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • meodanang
      New Member
      • May 2010
      • 6

      #3
      Any help please? Has anyone tried to load a page with javascript on it to a <div> and that javascript code still worked?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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.

        Comment

        • meodanang
          New Member
          • May 2010
          • 6

          #5
          Thank you Dormilich. I will find out the other way then

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            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

            Comment

            • meodanang
              New Member
              • May 2010
              • 6

              #7
              Originally posted by RamananKaliraja n
              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
              That was just an example that I wrote. I didn't copy my code because I couldn't access it at that time. :)

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                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:
                Code:
                var script = document.createElement("script");
                // set src and type
                script.src = ...
                head.appendChild(script);

                Comment

                Working...