[英]Jquery/Javascript: How to Remove JS file for a page which is not being used in current page
Basically I have 4 views in my page (4 different sections in a single HTML page), I am hiding and showing the required views based on the button click. 基本上,我的页面中有4个视图(单个HTML页面中有4个不同的部分),我将隐藏并显示基于按钮单击的所需视图。
I have 4 separate JS file for these 4 views. 对于这4个视图,我有4个单独的JS文件。 Based on the page user is in I am loading the JS page using the command
基于页面用户所在,我正在使用以下命令加载JS页面
//Page1
if (current_page == 'Page1') {
$.getScript("/js/Page1.js");
}
I want to remove the all unused JS (JS which are used for other pages) when user navigates to 2nd page and only Page2.js
should be loaded, Currently when user navigates to 2nd page (Page1 to Page2) both Page1.js
and Page2.js
are being loaded. 当用户导航到第二页并且仅应加载
Page2.js
时,我想删除所有未使用的JS(用于其他页面的JS);当前,当用户导航到第二页(Page1至Page2)时, Page1.js
和Page2.js
正在加载。
$.getScript
can load the JS but is there a command or way to remove the JS from page and load the JS file dynamically? $.getScript
可以加载JS,但是是否有命令或方法可以从页面中删除JS并动态加载JS文件?
Thanks for the response, I was able to figure out the issue and some workaround for that. 感谢您的答复,我能够找出问题所在,并找到解决方法。 Basically I had some common functions in Page1 JS and Page2 JS so I wanted some of the functions in my Page2 JS not to run when I am using Page1 but they were getting executed because of them were using same modal and form submit IDs.
基本上,我在Page1 JS和Page2 JS中具有一些通用功能,因此我希望在我使用Page1时,我的Page2 JS中的某些功能不运行,但由于它们使用的是相同的模式和表单提交ID,因此它们得以执行。
I declared a variable in the JS and Based on that I made sure the functions in Page2 JS not running when I am in Page1 我在JS中声明了一个变量,并据此确保了当我在Page1中时,Page2 JS中的功能未运行
var Page_Checker = "";
//Page1
if (current_page == 'Page1') {
$.getScript("/js/Page1.js");
Page_Checker = "THIS_IS_PAGE1"
}
Use the Page_Checker
and make sure the functions are not running in other JS files. 使用
Page_Checker
并确保功能未在其他JS文件中运行。 May be you can do something like this in other JS files: 也许您可以在其他JS文件中执行以下操作:
if(Page_Checker != "THIS_IS_PAGE1")
return;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.