生成不引人注目的 JS
我读过很多关于不引人注目的 JS 以及如何生成它和所有爵士乐的内容...
我的问题是:我有一个严重依赖 mod_rewrite
的网站,所以基本上所有页面请求都是发送到 index.php
,生成页面的主要结构,然后包含适当的页面。现在,网站中有不同的部分,每个部分都使用不同的 Javascript 函数(例如,针对不同的 AJAX 请求)。
现在,如果我只是将一个函数附加到页面的 onload
上,显然这件事是行不通的,因为我不必为每个页面初始化相同的东西......那么什么是处理这种情况的最佳方法是什么?
我希望情况清楚,如果需要,我很乐意澄清
I have read quite a bit about unobtrusive JS and how to generate it and all that jazz...
My problem is this: I have a website that heavily relies on mod_rewrite
, so essentially all the pages requests are sent to index.php
that generates the main structure of the page and then includes the appropriate page. Now, there are different sections in the site and each section uses different Javascript functions (e.g. for different AJAX requests).
Now, if I just were to attach a function to the onload
of the page obviously the thing would not work, as I do not have to initialise the same things for each page... so what is the best way to handle this situation?
I hope the situation is clear, I'll be happy to clarify if needed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
addEventListener
(标准)或 AttachEvent在由附属 PHP 页面生成的 HTML 中。语法很简单。例如,
这允许您在index.php 中生成完整的正文标记,但为每个页面运行不同的加载处理程序。另请注意,您可以在同一元素上多次使用它。
You can use
addEventListener
(standard) or attachEvent in the HTML generated by the subsidiary PHP pages.The syntax is simple. E.g.
This allows you to generate the full body tag in index.php but run different load handlers for each page. Also note that you can use this multiple times on the same element.
Nico,我建议为每个包含的页面创建自定义 JavaScript 代码(无论页面上的哪个位置包含脚本标记),并且正如 Matthew 建议的那样,在定义要在页面加载时运行的函数后,使用 addEventListener 来在“load”上加载该自定义函数
假设您在所包含文档的正文中的某个位置定义了一个函数 pageinit()
这对您的项目有意义吗?
Nico, I would suggest creating a custom javascript code with each included page (doesn't matter where on the page you include the script tag) and, as Matthew suggested, after you define a function to run on page load, use the addEventListener to load that custom function on "load"
Let's say you define a function pageinit() somewhere in the body of the included document
Does that make sense for your project?
我只是将其放入 .js 文件中。
mysite_common.js - 站点范围内的通用实用程序和函数
mysite_page_a.js - 页面 a 的独特功能
mysite_page_b.js - 页面 b 的独特功能 对于
页面 b,您包含 b.js,而在页面 a 上您将包含 a.js
然后在您各自的unique.js 你可以将你的功能包装在 ondomready 或类似的东西中。
将其与 PHP 分开,这样以后就不会那么麻烦了,这也意味着您可以依靠 js 的缓存来保持页面加载更轻。
您还可以查看 YUI 加载器之类的东西,它允许您执行更复杂的事情,例如按需加载 js 功能位。
您可以使用事件委托根据上下文提供不同的功能。
基本上,它的工作原理是将事件侦听器附加到容器元素,该元素捕获子元素的点击。然后,您可以完全取消单个事件侦听器,并查看来自父级的提示。
说:
然后
onload = function(){attachDelegates('container');};
AttachDelegates 函数对于每个页面可能不同,或者您可以有一个整体的函数和简单的附加提示到容器或者有选择性地选择您附加的课程。
这些是更加连贯的解释和示例:
http://cherny.com/webdev/70/javascript-事件委托和事件处理程序
http://blog.andyhume.net/event-delegation-without-javascript-库
我个人使用YUI3
http://developer.yahoo.com/yui/ 3/examples/node/node-evt-delegation.html,
因为它为我提供了 CSS3 样式选择器,并且到目前为止非常顺利。
I would simply put it in a .js file.
mysite_common.js - site wide common utils and functions
mysite_page_a.js - unique functionality for page a
mysite_page_b.js - unique functionality for page b
for page b you include b.js while on page a you would include a.js
Then in your respective unique.js you can wrap your functionality in a ondomready or similar.
Keep it separate from your PHP, then it is much less of an annoyance later, it also means that you can rely on caching for your js to keep your page loads slimmer.
You can also look at things like YUI loader which allows you to do much more complex things like ondemand loading of bits of js functionality.
You can use event delegation to provide different functionality depending on context.
Basically it works by attaching an event listener to a container element which captures clicks on child elements. You can then do away with individual event listeners alltogether, as well as look at hints from the parent.
say:
Then
and
onload = function(){attachDelegates('container');};
The attachDelegates function could be different for each page, or you could have a monolithic one and simple attach hints to the container or be selective about which classes you attach.
These are much more coherent explanations and examples:
http://cherny.com/webdev/70/javascript-event-delegation-and-event-hanlders
http://blog.andyhume.net/event-delegation-without-javascript-library
Personally I use YUI3
http://developer.yahoo.com/yui/3/examples/node/node-evt-delegation.html
as it gives me CSS3 style selectors and is pretty hassle free so far.