At 01:20 PM 1/13/05, Seona Bellamy wrote:
> -----Original Message-----
> [mailto:[EMAIL PROTECTED] Behalf Of Paul Novitski
> Subject: Re: [WSG] Popups
> Also, while it's convenient to insert javascript event handlers into HTML
> markup when demonstrating an example, in practice it's probably best to
> leave the script out of the markup and apply it from a separate
> script file
> at window.onload.

I'm curious as to how you do that, because to my mind it's a great idea.
Keeping it out of the markup would make sure that the code of the page
itself remains nice and lean and would also make it easier to remove the
popups altogether if such a feat was necessary.


Seona,

Here's a quickie example in which I assign Jeff's href event handler to a single specific hyperlink:

===============================
HTML:

<head>
<script type="text/javascript" src="assignevent.js"></script>
</head>
<body>
<a id="anchor1" href="http://example.com";>Open the example site</a>
</body>

===============================
JavaScript:

// this file is "assignevent.js"


// Tell javascript to run a function when the page finishes loading: window.onload = jsAssignEvent;


// When the page loads, assign the event handler to the object: function jsAssignEvent() { // don't run code the browser can't handle if (document.getElementById) { // get the object var oAnchor = document.getElementById("anchor1");

                // assign the event handler
                oAnchor.onclick = jsOpenLinkWindow;
        }
}


// When the link is clicked, open the new window: function jsOpenLinkWindow(evt) { // stop event propagation if (!evt) var evt = window.event; evt.cancelBubble = true; if (evt.stopPropagation) evt.stopPropagation();

        // open the link in a new window
        window.open(this.href, '_blank', 'width=500,height=500,menubar=no');

        // cancel the click event so the parent window location doesn't change
        return false;
}
===============================

I recommend Peter Paul Koch's articles on event handlers & javascript at http://www.quirksmode.org/

Cheers,
Paul


******************************************************
The discussion list for  http://webstandardsgroup.org/

See http://webstandardsgroup.org/mail/guidelines.cfm
for some hints on posting to the list & getting help
******************************************************



Reply via email to