简体   繁体   English

我如何在一个JavaScript文件中拥有多个document.observe(dom:loaded…)函数? (原型)

[英]How can I have multiple document.observe(dom:loaded …) functions inside one javascript file? (prototype)

Basically what I'm after is, following squashing all my JavaScript into one file (or a few files) how can I make a document.observe(dom:loaded({...})) function call for the right page? 基本上,我的追求是,在将所有JavaScript压缩到一个文件(或几个文件)之后,如何制作document.observe(dom:loaded({...}))函数调用正确的页面?

So, say I have 5 pages in my website, and each one has a separate JS file. 因此,假设我的网站上有5个页面,每个页面都有一个单独的JS文件。 Inside each file is a load of page related functions, and a special function which sets up eventhandlers etc when the DOM has loaded. 每个文件内部都有与页面相关的函数,还有一个特殊的函数,可以在DOM加载后设置事件处理程序等。 Obviously if all 5 pages include their own JS files then that's fine. 显然,如果所有5个页面都包含其自己的JS文件,那就可以了。 But I want to compact all my JS into one page for versioning and efficiency reasons. 但是出于版本控制和效率的原因,我想将我的所有JS压缩到一页。

Therefore, in doing this, I would end up with 5 "dom:loaded" functions waiting to be setup. 因此,这样做,我将得到5个“ dom:loaded”函数,等待设置。 This won't do as the stuff inside these functions is page specific. 这些函数中的内容是特定于页面的,因此不会这样做。 So, my question is how can I do what I want to do without causing a whole bunch of DOM errors, and false eventhandler setup requests? 因此,我的问题是如何在不引起大量DOM错误和错误的事件处理程序设置请求的情况下做我想做的事情?

Ive considered namespaces and stuff like that but don't really know anything about it/them. 我已经考虑过名称空间和类似的东西,但实际上对此一无所知。

Btw, I'm using the Prototype lib. 顺便说一句,我正在使用Prototype库。 Oh, and my sites are considerably larger than 5 pages! 哦,我的网站超过5页! :) :)

Thanks, Lee 谢谢李

Some ideas: inspect the dom to identify the page. 一些想法:检查dom以识别页面。 For example, attach an attribute to the body element. 例如,将属性附加到body元素。 put a script tag in the page that sets a variable. 将脚本标签放在设置变量的页面中。 inspect the url. 检查网址。

when the load event fires, the event handler identifies the page and hands control off to the proper code. 当加载事件触发时,事件处理程序将识别页面并将控件移交给正确的代码。

我将为单独的HTML文件的<body>提供不同的ID,并在dom:loaded事件中使用$$('body')[0].id测试<body>元素的名称。

I have opted to use a php regexp to capture the URI, then use this as the body ID. 我选择使用php regexp捕获URI,然后将其用作正文ID。 On my sites, where the page names are static, it means each page will have a unique ID. 在我的网站上,页面名称是静态的,这意味着每个页面都将具有唯一的ID。

I then include a JS file in the HEAD which contains a switch block inside which the appropriate code/functions are loaded. 然后,我在HEAD中包含一个JS文件,该文件包含一个开关块,在该开关块中加载了适当的代码/功能。 The switch block is inside the document.observe(dom:loaded...) function. 开关块位于document.observe(dom:loaded ...)函数内部。

Works a treat! 工作请客!

Thank you again for your help. 再次感谢你的帮助。

I tend to always write my .js with no self activation (so much as possible)... 我倾向于总是编写没有自我激活的.js文件(尽可能多)...

psuedo-code 伪代码

namespace('mysite.section.init');
mysite.section.init.pageName = function(){
  //stuff to run here...
};

in your home page, at the bottom, or via dom:loaded event simply run mysite.section.init.pageName(); 在您的首页,底部或通过dom:loaded事件,只需运行mysite.section.init.pageName();

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

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