Results 1 to 4 of 4

Thread: Inserting a new javascript function in body from ajax request

  1. #1
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Inserting a new javascript function in body from ajax request

    Hello,

    I am loading a page via ajax request into a div, in the page loaded i have a new javascript function that i'd like to load and call on but it doesn't seem to work:

    <script type="text/javascript">
    function testFun(){
    alert('Test Alert!');
    }
    </script>


    When the above ^^ is loaded in the div from the ajax request I can't call on it... any ideas? (not working in IE of FF)

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    The alert works fine so it's a problem with the ajax. Could you please post the javascript (ajax) you are using to include the page.

  3. The Following User Says Thank You to keyboard For This Useful Post:

    crobinson42 (03-18-2012)

  4. #3
    Join Date
    May 2010
    Location
    Sacramento, CA
    Posts
    91
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default

    Code:
    <script type="text/javascript">
    function ajaxRequest(divTag,url)
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      	//loading animation
    	document.getElementById([divTag]).innerHTML="<image src=../images/loading.gif></img>";
    	
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById([divTag]).innerHTML=xmlhttp.responseText;
    	parent.calcHeight();
        }
    
      }	
    xmlhttp.open("GET",[url],true);
    xmlhttp.send();
    
    }
    </script>

  5. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Non-native code that is present in pages fetched via Ajax doesn't work (anymore) if Ajax uses innerHTML or its DOM-alternative(s). In that case, you have to bring in the code manually (to the parent page). Code is only preserved automatically if you use document.write, as in:
    Code:
    <script type="text/javascript">
    
    function HttpRequest(url){
    var pageRequest = false //variable to hold ajax object
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
    pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
    }
    catch (e){
    try {
    pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
    }
    catch (e2){
    pageRequest = false
    }
    }
    @end
    
    @*/ if (!pageRequest && typeof XMLHttpRequest != 'undefined')
    pageRequest = new XMLHttpRequest()
    
    if (pageRequest){ //if pageRequest is not false
    pageRequest.open('GET', url, false) //get page synchronously
    pageRequest.send(null)
    document.write(pageRequest.responseText)
    
    }
    }
    HttpRequest('page_you_want_to_include.html')
    </script>
    This is synchronous page-inclusion, but it works most of the time.
    (keyboard1333, sorry to interrupt; I didn't see your comment when I posted my answer)
    ===
    Arie Molendijk.
    Last edited by molendijk; 03-18-2012 at 11:30 PM. Reason: Correction

  6. The Following User Says Thank You to molendijk For This Useful Post:

    crobinson42 (03-19-2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •