Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading javascript through ajax

I am fairly new to AJAX

I have a webpage (say webpage A) which upon a certain button press, calls webpage B through AJAX. webpage B is having 2 javascript files included. Now whenever I click the button, the javascript files are not being loaded. its correspoing CSS files are loaded successfully by AJAX. Here is what I got after inspection-

error is on line 7372 in jquery.js

Here arise my question 1- how is it making call to wdm.js? = some random number ??

Now, for the workaround, I have tried to load the javascript files by using jQuery.getScript() function using this code-

$.ajax({
     type: "POST", url: "webpageB.php", data: "id="+id,
         complete: function(data){
                           //some code

                           $.getScript('js/wdm.js');

                           // some code
                }
           });

still I am getting this error now-

Now my question 2 arises here- Why is that the random number, came out of nowhere, is appearing into ajax GET request?

I can not use the javascript on webpage A for some reasons, So the only option left is to load the javascripts dynamically (synchronously with DOM elements of webpage B). Can anyone suggest some method?

url still have that random number

Edited- using the function ajaxsetup do remove the random number appearing in the URL, but now I see the file is still not being loaded by ajax, even though chrome inspector(which I use to monitor the ajax calls) do not show any errors. Now the situation is bit complex.

like image 812
ashutosh Avatar asked Dec 08 '25 06:12

ashutosh


1 Answers

Random number is timestamp which is used to prevent browser's cache. If that's not what you want, add cache : true option in ajax call. For example: $.ajax({url: "url", success: callback, cache: true});. If you want to prevent all future AJAX requests from being cached by default(this is actually the default setting in jQuery):

$(document).ready(function() {
  $.ajaxSetup({ cache: true });
});
like image 99
Hui Zheng Avatar answered Dec 09 '25 18:12

Hui Zheng