The problem is that your script is trying to reference something that
doesn't exist yet. The script is intepreted inline with the parsing of
the page, so the 'bodyid' element doesn't exist as of when you try to
do something with it.

The dom:loaded version should have worked, because that specifically
what that event is for. You'd have to show up more of your actual (non
"blah blah") code for us to help with that bit. :-)

But basically:

document.observe("dom:loaded", pageLoad);

function pageLoad() {
    // read your cookie here

    // set up an unload handler to write it
    Event.observe(window, 'unbeforeload', pageUnloading);
function pageUnloading() {
    // write your cookie here

T.J. Crowder
Independent Software Consultant
tj / crowder software / com

On Jun 9, 3:08 pm, fernan <fernan.agu...@gmail.com> wrote:
> Hi!
> I need to be able to i) read a cookie when the page loads, and ii)
> save data into the cookie when the page unloads (i.e. when the user
> clicks and navigates to another page, within the same site or to a
> different site).
> I have working code (in a catalyst web application) using <body
> onload="readCookie();" onunload="saveCookie();">.
> However, we want to move this code out of the <body> element, so we
> can only use it in some pages (the <body> tag is added by a global
> template for the site). So I started playing around with prototype's
> event.observe ... and can't get it to work.
> I just cooked a minimal test page to check what the problem might be,
> and can't figure out what I'm doing wrong.
> I'd appreciate any help or direction ...
> Test page:
> <html>
>   <head><title>event.observe test</title>
>     <script type="text/javascript" src="prototype-1.6.1.js"></script>
>     <script type="text/javascript" language="JavaScript">
>       Event.observe('bodyid', 'load', function show1() { $
> ('div1').toggle() });
>       // have also tried document.observe("dom:loaded", function
> show1() { blah blah } );
>       // as well as
>       // Event.observe('body', 'load', function show1() { $
> ('div1').toggle() });
>       // Event.observe('bodyid', 'onload', function show1() { $
> ('div1').toggle() });
>       // etc.
>     </script>
>   </head>
>   <body id="bodyid">
>   <h1>Test</h1>
>   <div id="div1" style="display: none">
>     <p>hello world</p>
>   </div>
>   </body>
> </html>
> When loading this page, 'hello world' is never displayed. The
> javascript debugger (chrome, firebug) show no errors.
> Tested on Chrome (6.0.422.0), Firefox 3.6.3, Opera 10 (build 4585),
> all on Ubuntu 10.04 (Lucid, i386).
> What is the problem with my code?

You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to