dataTables.js file not storing in cache / Avoid request with query string

dataTables.js file not storing in cache / Avoid request with query string

kpaswinkpaswin Posts: 5Questions: 2Answers: 0

Whenever I refresh/load the page, along with the ajax request, datatables file also loads from the server.
Everytime it appends a query string along with the request (http://localhost/js/datatables/jquery.dataTables.min.js?_=1448124952048). So the date dataTables.min.js is not returning from cache and it makes request everytime I reload the page.This does not happen for any other js pages. This costs much in page load time. Is there a way to avoid query string. So that browser can cache js file.

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited November 2015

    Can you show the code you're using to load the js file?

    Is it just a script? or are you using headJS or requireJS? Is this in something like angular or some type of CMS? Details man! details!.. Preferably a way to replicate the "issue"

    I highly doubt this is a DataTables related issue though, because this is obviously related to how you're loading the jquery.dataTables.min.js file, not with what the file itself is doing.

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    That isn't something that DataTables is doing (indeed, DataTables hasn't loaded when you request it!). As jLinux says, this is related to however you are loading the file (or perhaps more probably, whatever loader you are using for the file).

    Allan

  • kpaswinkpaswin Posts: 5Questions: 2Answers: 0

    I want a datatable file only for a particular page (table.html). I called that page inside index.html using jQuery load function. I have called Datatables CSS & JS only inside that table.html .

    But I want to know why it does not work if called inside the ajax page.

    My code

    index.html

    <html>
    <head>
    </head>
    <body>
    <div class=container>
    
    </div>
    <script src="js/jquery-1.10.2.min.js"></script>
    //It works fine if I call datatables here
    <script>
    $(document).ready(function() {
            $(".container").load("table.html");
    </script>
    </body>
    </html>
    

    table.html

    <div>
    .
    .
    .
    .
    </div>
    <link href="css/jquery.datatables.css" rel="stylesheet">
    <script src="assets/datatables/jquery.dataTables.min.js"></script> 
    
    // It automatically sends param if called from here
    
    <script>
    $(document).ready(function() {
        var dataTbl = $('#table2').DataTable({
            "ajax": {
                "url": "contact.json",
                "type": "GET",
                dataSrc: ''
            },
            columns: [{
    .
    .
    .
    .
    .
    .
    });
    </script>
    
  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    Answer ✓

    Wait, you're loading the entire file (CSS and JS file tags and all), via AJAX? And your'e wondering why its not working?

    Think about it.. For starters.. I believe that the $().load() basically just grabs the content of the page, and throws it in whatever the target is.. Meaning the JS/CSS files you include shouldn't work. I mean I haven't ever tried it before, because it's just not the right way to do it.

    Also, DataTables manages the table through the DOM, and the $().load() wont process the jQuery thats on the page and throw it into the DOM.

    Why are you trying to do it like this anyways? It's definitely a terrible way of accomplishing whatever it is you're trying to accomplish

  • kpaswinkpaswin Posts: 5Questions: 2Answers: 0

    Sorry for it. I am a beginner.

    I thought it would be a bad idea to import it in the index.html since imoprt would be useless if the user does not visit table.html.

    Thank u.

This discussion has been closed.