简体   繁体   English

如何在每个HTML页面上分别执行jQuery / Ajax加载

[英]How to execute jQuery/Ajax load on each html page individually

Please I have few html pages that i include some js scripts after the body . 请在主体后面添加一些html脚本的HTML页面。 My script are working fine and successful. 我的脚本运行正常且成功。 However, it gets messy putting the two together as the project grow. 但是,随着项目的发展,将两者放在一起会很麻烦。 I then took the js in to an individual files . 然后,我将js放入单个文件中。 The problem is, now i reference all the files and the js codes became available for each and every html page. 问题是,现在我引用了所有文件,并且js代码可用于每个html页面。

I have events where I check each page load , because i want to execute or start my file on the page load . 我有检查每个页面加载的事件,因为我想在页面加载时执行或启动文件。 Now this page loads fire on every page load . 现在,此页面在每次加载页面时都会触发。 Example below, 下面的例子

(function() {

    $(this).on('load', function(){
        console.log("the init is");
        var g = true;

        if(g    === false)
            window.location = '/';

        init();
    });


    var init = function(){
        $('#btnAdd').on('click',function(e){
            e.preventDefault();
            e.stopPropagation();
            alert("Butoon click");
        });
    };

})();

Before when I embed a similar code above, it fires when my page is executed and my load became the entry point as I wanted . 在我在上面嵌入类似的代码之前,它会在我的页面执行并且我的负载成为我想要的入口点时触发。 But now I moved them to separate html file and reference them which most have the same or similar functions. 但是现在我将它们移到单独的html文件中,并引用它们中大多数具有相同或相似功能的文件。 Now when i visit very html page, the onload method fires regardless , because they are all reference and available for each page. 现在,当我访问非常html的页面时,无论,都会触发onload方法,因为它们都是参考,可用于每个页面。

Please is there any way I can refactor my code to separate every js file for a separate html page/url. 请以任何方式我可以重构代码以将每个js文件分离为单独的html页面/ URL。 How do I make jQuery or ajax load call with reference to the url? 如何参考URL进行jQuery或Ajax加载调用? How do I make each file fires when its respective html/url is requested from server or loading? 从服务器请求或加载文件时,如何使每个文件触发? Any help would be appreciated 任何帮助,将不胜感激

There are a couple common approaches to executing different page initialization code on different pages. 有两种常见的方法可以在不同的页面上执行不同的页面初始化代码。

  1. You can break the page-specific initialization code and supporting code into separate script files and only include them in <script> tags on the actual pages where they are needed. 您可以将特定于页面的初始化代码和支持代码分解为单独的脚本文件,并将它们仅包含在需要它们的实际页面上的<script>标记中。

  2. You can use a common script file that is loaded across many pages and then within the page initialization code, you can check which page is loaded and then decide in your code which code is appropriate to run. 您可以使用跨多个页面加载的通用脚本文件,然后在页面初始化代码中,可以检查加载了哪个页面,然后在代码中确定适合运行的代码。

  3. You can use a single common script file that includes a separate function for each separate page that needs unique initialization, but no code that calls those functions and then add one unique initialization function call to each individual page in the body of the page. 您可以使用一个公共脚本文件,该文件为需要单独初始化的每个单独页面包含一个单独的函数,但是没有代码调用这些函数,然后向该页面主体中的每个单独页面添加一个唯一的初始化函数调用。 This allows for the most efficient browser caching of script files and loading of fewer script files (it lets you combine and minimize most of your scripts). 这允许浏览器对脚本文件进行最有效的缓存,并加载更少的脚本文件(它使您可以组合和最小化大多数脚本)。

For the second option, you can detect which page is loaded in your Javascript a number of different ways. 对于第二个选项,您可以通过多种不同的方法来检测Javascript中加载了哪个页面。

  1. You can detect a class name on a known tag (like the body tag). 您可以在已知标签(如body标签)上检测类名。
  2. You can set a unique JS variable in each page that the initialization code can test. 您可以在初始化代码可以测试的每个页面中设置唯一的JS变量。
  3. You can look for the existence of certain HTML tags in the page that uniquely indicate which type of page it is. 您可以在页面中查找某些HTML标记的存在,以唯一地指示它是哪种页面类型。 This works well when you want a particular initialization function to run on a bunch of pages that all have a certain common element. 当您希望特定的初始化函数在所有具有特定公共元素的页面上运行时,此方法很好用。

Here's an example of option 1 (detect class name on body tag). 这是选项1的示例(在body标签上检测类名)。

var m = document.body.className.match(/pageType_([^\s]+)/);
if (m) {
    switch(m[1]) {
        case "home":
            // code here
            break;
        case "category":
            // code here
            break;
        case "product":
            // code here
            break;
        default:
            // code here
            break;
    }
}

Here's an example of option 2 (set unique JS variable in each page): 这是选项2的示例(在每个页面中设置唯一的JS变量):

    <script>
    // somewhere in each unique page
    var pageType = "home";
    </script>

    <script>
    // common script file
    switch(pageType) {
        case "home":
            // code here
            break;
        case "category":
            // code here
            break;
        case "product":
            // code here
            break;
        default:
            // code here
            break;
    }
    </script>

Here's a scheme for detecting which unique tag exists and calling the appropriate initialization function: 这是一种检测存在哪个唯一标记并调用适当的初始化函数的方案:

 if (document.getElementById("home")) {
      initHome();
 } else if (document.getElementById("category") {
      initCategory();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM