Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post

    jQuery .load() not loading script tags

    Hello Friends .

    i am using jQuery .load() method to load data from another page ....
    its working good ... but its only loading the HTML content .... i have many <script> tags in target page .... but script tags are not loading ... here is my JQuery code

    Code:
    jQuery(function(){
     $("body").on('click','a[rel=int]',function(e){
    	e.preventDefault();
    	var link = $(this).attr("href");
    
    		window.location.hash = link;
    	
    	});
    	
    $(window).bind('hashchange',  function(){
    $("#content").html("<div class='searching'><img src='img/searching.gif'></div>");
    theHash = window.location.hash.substring(1);
    
    	
    	$("#mainarea").load(theHash+" #main");
    
    	});	
    
     $(window).trigger("hashchange");
     
    });

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Based on jQuery load() documentation:

    Here, any JavaScript loaded into #a as a part of the document will successfully execute.

    Code:
    $( "#a" ).load( "article.html" );
    However, in the following case, script blocks in the document being loaded into #b are stripped out and not executed:

    Code:
    $( "#b" ).load( "article.html #target" );
    You are using the second one.

    It's always good to refer to the documentation.

    http://api.jquery.com/load/#script-execution

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    glenngv is there any solution get scripts executed .. with second method .. ?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    If jQuery says so, then I don't think you can. You have to use $.ajax() which is what load() is internally using.

    Btw, if you look at the jQuery source for the load() method at: http://james.padolsey.com/jquery/#v=...jQuery.fn.load

    you will see the reason why they excluded the scripts in the second method with selector.

    This is the code inside the ajax done callback.

    PHP Code:
    self.html(selector ?

        
    // If a selector was specified, locate the right elements in a dummy div
        // Exclude scripts to avoid IE 'Permission Denied' errors
        
    jQuery("<div>").append(jQuery.parseHTML(responseText)).find(selector) :

        
    // Otherwise use the full result
        
    responseText); 


  •  

    Posting Permissions

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