SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CBE and the windowOnload() function

    For all those enthusiastic fans of the CBE API like myself, I have the following question.

    The CBE is setup in such a way that you need to define a specific function called windowOnload() in order to initialize whatever settings you like, for instance the size, background color and position of a given element. This is picked up automatically and called by the CBE framework when your web page is loaded and ready to go. This works just fine if you need a per page initialization independent from the rest of your site.

    My problem is this. I use SSI to pull in a windowOnload() function for one-time global configuration of CBE stuff generic to the whole site. For example, sliding the page title from left to right. This happens on every page and as expected works just fine.

    Now I want to add a special effect or some other CBE object initialization which is unique to a given web page. If I do this using the windowOnload() function by defining it in the specific page, this particular effect works fine, but cancels the generic effect of the sliding page title. That's because the generic windowOnload() is simply lost due to the simple fact that the function is being overwritten by the local version.

    My question then is this: how can I combine a generic windowOnload() function with a specific per page windowOnload() function so that both functions will be called, thereby retaining the global as well as the local CBE stuff?

    You get something like this:

    Page #1
    --------
    windowOnload() /* SSI */
    windowOnload_page1()
    Page #2
    --------
    windowOnload() /* SSI */
    windowOnload_page2()

    Etc.

    Do any clever CBE-folks out there have any clues?
    Kiffin
    Your average future-famous kind of guy...

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Kiffin,

    I'd also like to hear any new ideas on this. The last time we discussed it the following was suggested.

    In the following example I assume...
    - the Head element is not in the included file.
    - the page-specific onload function is named the same in each page. In this example - myOnload().
    Code:
    <html>
    <head>
    <!--#include virtual="/includes/header.htm"#-->
    <script type="text/javascript">
    function myOnload() {
    ...
    }
    </script>
    </head>
    <body>
    ...
    </body>
    </html>
    At the end of the windowOnload function (in the included file), do something like this...
    Code:
    <script type="text/javascript">
    function windowOnload() {
      ...
      if (window.myOnload) myOnload();
    }
    </script>
    And there could be variations of the above.

    Actually I think the only problem is when the local onload handler is assigned in the opening Body tag. The following is an idea for 'cascading' the onload handlers.

    The following is CBE's onload handler (in cbe_core.js):
    Code:
    window.onload = function() {
      cbeInitialize("DIV", "SPAN");
      if (window.windowOnload) window.windowOnload();
    }
    For my experiment I replaced the above with this:
    Code:
    window.originalOnload = window.onload;
    window.onload = function() {
      cbeInitialize();
      if (window.windowOnload) window.windowOnload();
      if (window.originalOnload) window.originalOnload();
    }
    It saves a reference to the current onload handler, so it can call it later during the onload event.

    This works but it has one ugly requirement... if you are going to define your own window.onload then cbe_core.js can only be included after your definition. For example:
    Code:
    <html>
    <head>
    <script>
    window.onload = function() {
      alert('in my onload');
    }
    </script>
    <script src='cbe_core.js'></script>
    </head>
    <body>
    </body>
    </html>
    And here's the example when you define window.onload through the body attribute:
    Code:
    <html>
    <head>
    <script>
    function myOnload() {
      alert('in my onload');
    }
    </script>
    </head>
    <body onload='myOnload()'>
    ...
    <script src='cbe_core.js'></script>
    </body>
    </html>
    I really don't like including cbe_core.js in the body element, but this is still the only suggestion I have for when the local handler is defined thru the body attribute.

  3. #3
    SitePoint Enthusiast kgish's Avatar
    Join Date
    Dec 2001
    Location
    Gouda, The Netherlands
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually Mike, I am familiar with this technique. Rather, I was looking for a more elegant solution, like for example registering the onload routines beforehand with some kind of CBE onloadRegister(functionx) routine.

    Any ideas on that?
    Kiffin
    Your average future-famous kind of guy...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •