Avoid jQuery Mobile to force script / CSS reload with _ = TIMESTAMP query string parameter

As far as I know, if you want to load JavaScript or CSS files along with a special page that automatically loads via ajax, then you should put the CSS / JavaScript links in the <div data-role="page"> container.

Example:

 <div data-role="page" data-theme="e"> <script type="text/javascript" src="/js/jquery/plugins/plugins.js"></script> 

All in all, this works great. However, somewhere along the way, the script url changes:

 /js/some_sepcial_script.js becomes eg js/some_sepcial_script.js?_=1299308309681 

Where 1299308309681 is the current Unix timestamp, which changes with every request and thereby prevents caching. I'm sure this is the intended behavior, but does anyone know how you can prevent the timestamp from being added to script / CSS URLs if you want to make the file cacheable?

+4
source share
3 answers

You tried:?

 $.ajax ({ // Disable caching of AJAX response */ cache: false }); 

It must globally modify ajax requests. I'm just not sure about external scripts.

[EDIT]

This is the source code for jquery mobile 1.0a3:

 var all = $("<div></div>"); //workaround to allow scripts to execute when included in page divs all.get(0).innerHTML = html; to = all.find('[data-role="page"], [data-role="dialog"]').first(); //rewrite src and href attrs to use a base url if( !$.support.dynamicBaseTag ){ var newPath = path.get( fileUrl ); to.find('[src],link[href]').each(function(){ var thisAttr = $(this).is('[href]') ? 'href' : 'src', thisUrl = $(this).attr(thisAttr); //if full path exists and is same, chop it - helps IE out thisUrl.replace( location.protocol + '//' + location.host + location.pathname, '' ); if( !/^(\w+:|#|\/)/.test(thisUrl) ){ $(this).attr(thisAttr, newPath + thisUrl); } }); } 

Nothing out there adds a cache prevent option.

[EDIT 2]

I know this is related to troubleshooting, but you tried to dynamically load js as described here: http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

(I know this can be done through jQuery, but for testing purposes I am trying to avoid jQuery)

+3
source

If I turn on jQuery 1.4.3 instead of 1.5, everything will be fine. This is a sufficient solution for me. Thanks again for your support.

+1
source

Try to run:

 $.ajaxPrefilter("script", function (s) { if (s.cache === undefined) { s.cache = true; } }); 

Has this behavior changed?

0
source

Source: https://habr.com/ru/post/1342397/


All Articles