Skip to main content

Bug Tracker

Side navigation

#8298 closed bug (invalid)

Opened February 17, 2011 02:46AM UTC

Closed February 17, 2011 11:52AM UTC

Last modified March 14, 2012 11:43AM UTC

jQuery version 1.5 - ajax - <script> tag timestamp problem

Reported by: norberttamas Owned by:
Priority: low Milestone: 1.next
Component: manipulation Version: 1.5
Keywords: Cc:
Blocked by: Blocking:
Description

See description here:

http://stackoverflow.com/questions/5022557/jquery-version-1-5-ajax-script-tag-timestamp-problem


[copied from link]\\\\

If I load some content with ajax (jQuery) which has a script tag in it, jQuery 1.5 adds the timestamp to the script tag src url. See example bellow.\\\\

Example: content what I load with ajax:

<div>text1</div>
<script type="text/javascript" src="/js/abc-xyz.js?r=1.1"></script>

This is the src url from where it loads the script code after I insert the previous content to the page:

.../js/abc-xyz.js?r=1.1&_=1297892228466

Does anybody knows why this happening?\\\\

It happens only with jQuery 1.5. It doesn't happen with jQuery 1.4.4.\\\\\\

Code Example:

$.ajax({
    url: content.html,
    type: 'GET',
    data: someDataObject,
    success: function(data) {
        // some code here
    },
    error: function(data) {
        // some code here
    }
});

Thanks.

Attachments (0)
Change History (1)

Changed February 17, 2011 11:52AM UTC by jitter comment:1

_comment0: Thanks for taking the time to contribute to the jQuery project by writing a bug report. The next time also please include the actual report and a test case on http://jsfiddle.net instead of only linking to an external site. \ \ After checking your report and your code samples I come to the conclusion that this isn't a bug. I also made this [http://jsfiddle.net/jitter/kdxGp/ test case] \ \ jQuery 1.4+ (until 1.5) had a bug which caused the `cache` option not to default to `false` for script requests. This bug (see #7578) has been fixed in 1.5 . \ \ Now what you might know or not know is, that jQuery does special-handle script tags when doing DOM manipulations (to prevent certain errors in IE). It filters them out and requests them via ajax. This explains why even a "normal" inline script tag suddenly is requested with additional url parameters. \ \ There are ways to work around this if it has unwanted side effects for you. \ \ - use `$.ajaxSetup({ cache: true })` when appropriate \ - use a [http://api.jquery.com/extending-ajax/#Prefilters prefilter] for script requests and e.g. check for urls where you don't want the random parameter to be added and set `cache: true` in the prefilter for those \ - in e.g. the success call back handle the script tags yourself by doing something along these lines \ {{{ \ var elems = $(htmlwithscripttags); \ elems.filter("script") //now do whatever with the scripts \ elems.filter(":not(script)").appendTo("body"); //e.g. \ }}}1297952376543568
component: unfiledmanipulation
description: See description here: \ http://stackoverflow.com/questions/5022557/jquery-version-1-5-ajax-script-tag-timestamp-problemSee description here: \ http://stackoverflow.com/questions/5022557/jquery-version-1-5-ajax-script-tag-timestamp-problem \ \ ----- \ [copied from link]\\\\ \ If I load some content with ajax (jQuery) which has a script tag in it, jQuery 1.5 adds the timestamp to the script tag src url. See example bellow.\\\\ \ Example: content what I load with ajax: \ {{{ \ <div>text1</div> \ <script type="text/javascript" src="/js/abc-xyz.js?r=1.1"></script> \ }}} \ This is the src url from where it loads the script code after I insert the previous content to the page: \ {{{ \ .../js/abc-xyz.js?r=1.1&_=1297892228466 \ }}} \ Does anybody knows why this happening?\\\\ \ It happens only with jQuery 1.5. It doesn't happen with jQuery 1.4.4.\\\\\\ \ Code Example: \ {{{ \ $.ajax({ \ url: content.html, \ type: 'GET', \ data: someDataObject, \ success: function(data) { \ // some code here \ }, \ error: function(data) { \ // some code here \ } \ }); \ }}} \ Thanks.
priority: undecidedlow
resolution: → invalid
status: newclosed

Thanks for taking the time to contribute to the jQuery project by writing a bug report. The next time also please include the actual report and a test case on http://jsfiddle.net instead of only linking to an external site.

After checking your report and your code samples I come to the conclusion that this isn't a bug. I also made this test case

jQuery 1.4+ (until 1.5) had a bug which caused the cache option not to default to false for script requests. This bug (see #7578) has been fixed in 1.5 .

Now what you might know or not know is, that jQuery does special-handle script tags when doing DOM manipulations. It filters them out and requests them via ajax. This explains why even a "normal" inline script tag suddenly is requested with additional url parameters.

There are ways to work around this if it has unwanted side effects for you.

  • use $.ajaxSetup({ cache: true }) when appropriate
  • use a prefilter for script requests and e.g. check for urls where you don't want the random parameter to be added and set cache: true in the prefilter for those
  • in e.g. the success call back handle the script tags yourself by doing something along these lines
var elems = $(htmlwithscripttags);
elems.filter("script") //now do whatever with the scripts
elems.filter(":not(script)").appendTo("body"); //e.g.