Best way to execute js only on specific page

前端 未结 4 1874
慢半拍i
慢半拍i 2020-11-28 22:13

I was wondering what would be the best way to execute a java-script code only on specific pages.

Let\'s imagine we have a template-based web-site, rewrite rule for t

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 22:28

    A little different approach than checking the URL path : You can group page specific event handlers in a single function and then in each include, have a domready which will call these functions.

    Eg: in script.js you have two functions (outside domready) viz. onPage1Load() and onPage2Load().

    While in your page1.php you have a $(document).ready(onPage1Load) and so on for other pages. This will make sure that unintended event handlers are not registered.

提交回复
热议问题