Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery html() getting Javascript (.js) with ?_=1365695139815

When I get an url that contains <script src="some-file.js"></script> as the follow example:

<html>
<script>
$(document).ready(function(){
    $.get('/some-url', function(r) {
        $('#html-container').html(r); // Contains: <script src="some-file.js"></script>
    });
});
</script>

<body>
<div id="html-container"></div>
</body>

This is result as sayed before in a autoload from some-file.js as result from /some-url than jQuery will add ?_={random number} to query string.

Resulting in the request: GET some-file.js?_=1365695139815

How can I disable this random request append from autoload from html() parse?

@edit

Since I can't make the request, it's because they are executed by html parse, with the Brian's answer I found this simple solution:

$.ajaxSetup({
    // Enable caching of AJAX responses
    cache: true
});

Found at How to set cache: false in jQuery.get call

like image 658
Ragen Dazs Avatar asked Sep 16 '26 13:09

Ragen Dazs


1 Answers

The jQuery appended query string is a result of jQuery's cache busting for ajax calls. To disable this, use the following:

$.ajax({
    url: '/some-url',
    cache: true,
    success: function(r) {
        $('#html-container').html(r); // Contains: <script src="some-file.js"></script>
    }
});

cache: true being the important part.

like image 101
Brian Hadaway Avatar answered Sep 19 '26 03:09

Brian Hadaway



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!