Everything, I knew that we can use the load method to load dynamic content into a div. But in my case, I found a problem when using it. If you want to load the same page that contains some external js files on the same page. you will find that the same js will be downloaded twice. one the first time to open the page, the second when ajax loads the same page. therefore, I think that in this case there will be a potential problem. for example, if you set a value for a variable in JavaScript that is initialized in a js file, after loading the page, you will find that it will be returned to the initialized value. One solution I can find is to use an IFrame, Is there any good way to do this? thanks.
The code will look below.
in test.html
<html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="test.js"></script> <script type="text/javascript"> $(function(){ $("#loadBtn").on("click", function(event) { $('#container').load('test.html'); }); $("#getTest").on("click", function(event) { alert(test); }); $("#changeTest").on("click", function(event) { test="changed"; }); }); </script> </head> <body> <div id="container" style="width:200px;height:200px; padding:10px; border:1px solid black;"></div> <input type="button" id="loadBtn" value ="Load"/> <input type="button" id="getTest" value="gettest"/> <input type="button" id="changeTest" value="changetest"/> </body> </html>
in test.js
var test="1";
source share