How do I stop jquery appending a unique id to scripts called via ajax?

后端 未结 3 675
我在风中等你
我在风中等你 2020-12-19 08:11

I\'m using jquery\'s ajax functions to grab a page fragment and display in a section of a page - this fragment includes html and references to external js files.

The

相关标签:
3条回答
  • 2020-12-19 08:38

    This solution is less of a hack than Murdoch's solution:

    $.ajaxPrefilter('script', function(options) {
        options.cache = true;
    });
    

    See http://api.jquery.com/jQuery.ajaxPrefilter/

    0 讨论(0)
  • 2020-12-19 08:44
    1. Force the webserver to serve the script with a expire date in the future.

    2. If you use dataType = "script" as an option for jquery ajax the caching will be disabled by default, see http://docs.jquery.com/Ajax/jQuery.ajax#options, try set it manually to "html".

    $.ajax({
      type: "GET",
      url: "test.js",
      dataType: "html" // if not set jquery will guess what type it is and disables caching when matching "script"
    });
    
    0 讨论(0)
  • 2020-12-19 08:46

    Looks like jQuerys's evalScript function is messing you up...

    Line 543 of jQuery:

    function evalScript( i, elem ) {
        if ( elem.src )
            jQuery.ajax({
                url: elem.src,
                async: false,
                dataType: "script"
            });
    
       else
            jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
    
       if ( elem.parentNode )
           elem.parentNode.removeChild( elem );
    }
    

    Here is the breakdown of what happens: The Main Page's JS calls:

    $.ajax({
        url:"frag.htm",
        type:"GET",
        success:callBackFunction
    })
    

    and GETs frag.htm which contains something like this:

    <html><head><script src="test.js"></script></head><body>Content</body></html>
    

    then your callback function is called which probably looks like this:

    function callBackFunction(data){
        $("#ajaxContent").html(data); // <- this is the beginning of your problems...
    }
    

    When jQuery's html(data) function is called is "cleans" the HTML by removing any script tags and then calls evalScript on each one. evalScript, as you can see, doesn't specify "cache:true" so when it goes through $.ajax cache is null. When cache is null and the dataType is "script" jQuery sets cache=false.

    So, to circumvent this problem try this:

    function callBackFunction(data){
        var tempAJAX = $.ajax; // save the original $.ajax
            $.ajax=function(s){ // wrap the old $.ajax so set cache to true...
                s.cache=true;
                tempAJAX(s); // call old $.ajax
            }
            $("#ajaxContent").html(data); // insert the HTML and download the <script>s
            $.ajax = tempAJAX; // reset $.ajax to the original.
        }
    }
    

    Before we insert the new HTML from "frag.htm" into the Main Page we intercept all calls to $.ajax, modify the object to include cache=true, and then after the script is loaded insert the HTML.

    Let me know if you have any questions.

    0 讨论(0)
提交回复
热议问题