Using Prototype to Load Javascript Files

來源:互聯網
上載者:User

The calendar on this site only appears on pages that show blog-related information. That calendar is enhanced with Javascript allowing you to change the month displayed by the calendar without reloading the rest of the page. So, in order to ensure that these enhancements would be available everywhere the calendar is, I figured I had two options:

  1. Code the inclusion of the Javascript file into every page which requires it. While a good solution, and one that has worked well in the past, sometimes it can be difficult to remember or realize that you've forgotten to include the file when you add new pages that require it.
  2. Find a way to automatically include the file when it's needed. This would avoid the need to remember the need to include it when adding new pages that require it, but would result in a little more Javascript going on when the page loads.

This past weekend, with a little help from the Prototype Framework, I found a solution for #2 above which I am, for the moment, quite pleased with. The following snippet assumes the most recent version of Prototype as of this writing (1.6.0.2):

document.observe("dom:loaded", function() {    var calendar = $("calendar");    if(calendar) {        var script = new Element("script", { type: "text/javascript", src: "/path/to/calendar.js" });        document.observe("calendar:loaded", function() { new Calendar(calendar); });        $$("head")[0].insert(script);    }});

Here's, essentially, what it does:

  1. Uses the custom dom:loaded event which, since Prototype 1.6.0, determines when the DOM is ready for manipulation to call the anonymous function defined in the snippet.
  2. That function attempts to get a reference to the calendar. If it can't, then nothing else happens. This makes sure that we don't include the calendar Javascript unless we need to.
  3. Once we've found a calendar:
    1. Create a new
    2. Tell the document to observe the calendar:loaded event, which is fired from the bottom of teh calendar's javascript.
    3. Insert the javascript file into the head of the document.

It's #3.2 that is the most important step. The calendar.js file makes sure to use the Prototype fire() method to create the custom calendar:loaded event. Thus, when the browser is done performing the necessary work required to insert the calendar.js file, it'll fire the custom event, which will be caught by the document object to instantiate the newly inserted Calendar.

In other words, with a creative use of Prototype's Event.observe() and Element.fire() methods, we not only include Javascript files only when they're needed, but also ensure that we don't attempt to use the code within those files until the browser is ready for it to be used.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.