简体   繁体   English

如何从一个文件执行JavaScript,尤其是页面?

[英]How do I execute JavaScript, page specifically, from one file?

I have a single file, 'core.js', with a collection of my sites JavaScript. 我只有一个文件“ core.js”,其中包含我的网站JavaScript的集合。 I like to combine my script this way so I can cut down on HTTP requests. 我喜欢以这种方式组合脚本,以便减少HTTP请求。 My problem is, page specific code's, obviously, getting run on pages it needn't. 我的问题是,页面特定的代码显然可以在不需要的页面上运行。

Without splitting 'core.js' into separate scripts, what's the best solution for ensuring my page specific code only gets run on the page it's supposed to be run on? 在不将“ core.js”拆分为单独的脚本的情况下,确保我的页面特定代码仅在应该运行的页面上运行的最佳解决方案是什么?

Many thanks! 非常感谢!

The simplest way is to make everything in core.js be functions and then put one inline function call in each given page to call the code specific to that page. 最简单的方法是使core.js中的所有内容成为函数,然后在每个给定页面中放置一个内联函数调用以调用特定于该页面的代码。 That makes core.js a resource and each page decides what functions in that resource to call. 这使core.js成为资源,每个页面决定该资源中要调用的函数。

I've also seen it done where you put a trigger class name on the body tag and then the code in core.js examines the classes on the body tag to decide what initialization code to run. 我还看到了将触发器类名放在body标签上,然后core.js中的代码检查body标签上的类以确定要运行的初始化代码的方法。 This works best when you have a small number of types of pages and you want to run the same initialization code on all pages of the same type, but I don't think it's all that good if each page has different JS. 当页面类型较少并且要在相同类型的所有页面上运行相同的初始化代码时,这种方法效果最好,但是如果每个页面具有不同的JS,我认为这并不是很好。 In that case, I think it's better to use the first technique of let the page decide what JS initialization function to call. 在这种情况下,我认为最好使用让页面决定要调用的JS初始化函数的第一种技术。

暂无
暂无

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

相关问题 我无法让我的 firefox 浏览器扩展工作 - 特别是让 javascript 文件在页面加载时执行 - I can't get my firefox browser extension to work - specifically getting the javascript file to execute on page load 如何从HTML元素执行javascript函数并发送该元素名称,而无需专门使用其名称? - How do I execute a javascript function from an HTML element and send that elements name without use its name specifically? 如何执行在另一页中写在一页中的JavaScript? - How to execute JavaScript writen in one page from another page? 使用Rails,如何在应用程序的特定页面上执行javascript文件? - Using Rails, how do I execute a javascript file on a specific page in the application? 如何从 HTML 表中提取数据,特别是<td id="word"> ,使用 JavaScript? - How do I pull data from a HTML table, specifically <td id="word">, using JavaScript? 如何从 Firefox 扩展执行页面定义的 JavaScript 函数? - How do I execute a page-defined JavaScript function from a Firefox extension? 如何将变量从一个 javascript 文件传递到另一个文件? - How do I pass a variable from one javascript file to another? 如何通过修改HTML页面的URL来执行javascript函数? - How do I execute a javascript function by modifying the URL of an HTML page? 如何在JavaScript页面加载中执行背景位置 - How do I execute background-position on a JavaScript page load 如何创建指向页面的链接并强制它立即执行 javascript - How do I create a link to a page and force it to immediately execute a javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM