简体   繁体   English

jQuery / Javascript:如何删除当前页面未使用的页面的JS文件

[英]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.jsPage2.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文件?

Here's a good post that explains it, but hat you can do is remove the <script> element you are not using: 这是一篇很好的说明文章,但是您可以做的就是删除不使用的<script>元素:

<script id="page1script" src="/js/Page1.js"></script>

And in jQuery: 在jQuery中:

if (current_page != 'Page1') {
    $("#page1script").html().remove();
}

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.

相关问题 哪些javascript文件未在页面加载时使用 - which javascript files are NOT being used on page load 如何知道网页上使用了什么Javascript(行和文件) - how to know that what Javascript (line & file) is being used on a Web Page 识别页面上实际使用了哪些外部javascript引用的工具 - Tool to identify which external javascript references are actually being used on a page 我可以排除在页面上使用 javascript 文件吗? - Can I exclude a javascript file from being used on a page? jQuery和Javascript-如何从页面中删除字符串? - Jquery & Javascript - how to remove strings from the page? 如何识别网页中正在使用的JQuery版本 - How to identify JQuery version being used in a web page 如何找到和查看页面上正在使用的Javascript? - How can I locate and view Javascript that's being used on a page? 如何从JavaScript文件检查正在使用哪个HTML? - How to check which HTML is being used from JavaScript file? 在Web浏览器中,JavaScript是否有可能获取有关当前页面使用的HTTPS证书的信息? - Within a web browser, is it possible for JavaScript to obtain information about the HTTPS Certificate being used for the current page? Fetch in JS自动添加当前页面url,如何去掉 - Fetch in JS adds automatically the current page url, how to remove it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM