Also, if you assign the event using the way below (detailed by Mark) the keyword
"this" would refer to the link so you could do this:

anchor.onclick = function(){
window.open(this.href,'popupwindow','width=400,height=400,scrollbars=1,resizable=1');
return false;
}

rather than using event.srcElement to get the href.  DO NOT USE attachEvent
unless you only intend this event to occur for IE.  It's non-standard - we
should be working to get rid of non-standard JavaScript as well as HTML and
CSS.  Finally, theres no need to use setAttribute().  The better way is to
reference the property directly as each link is also a Link object that makes
these properties available.  It's simpler and more reliable cross browser. 
Here's how Id rewrite it:

<script type="text/javascript">
>         var anchors = document.getElementsByTagName('a');
>         for (var i=0; i<anchors.length; i++) {
>                 if (anchors[i].rel == 'help') anchors[i].onclick =
function(){
window.open(this.href,'popupwindow','width=400,height=400,scrollbars=1,resizable=1');
return false;
};  
>         }
> </script>

I think now we've established standards and good practice for HTML and CSS, DOM
scripting is still overlooked.  PPK has established some pretty good practices
but we need to go further.  If anyone else has an interest in this get in
contact as I'd like to take it forward in some way.

Cheers,

Dan Webb
http://www.danwebb.net

Quoting Mark Lynch <[EMAIL PROTECTED]>:

> Hi Justin,
> 
> You can also use the simpler event model and add the event as follows:
> 
> anchor.onclick = function(){
>   alert('anchor with rel clicked');
> }
> 
> This works in both IE and Mozilla.
> 
> For more info on events in javascript the best resource I've found is
> http://www.quirksmode.org
> 
> Cheers,
> Mark
> 
> On Thu, 19 Aug 2004 10:06:28 +0300, Eser 'Laroux' <[EMAIL PROTECTED]>
> wrote:
> > You can use attachEvent method for this. But it's supported by Internet
> > Explorer 6 only.
> > 
> > --
> > <a href="about:mozilla" rel="help">test</a>
> > 
> > <script type="text/javascript">
> >         var anchors = document.getElementsByTagName('a');
> >         for (var i=0; i<anchors.length; i++) {
> >                 var anchor = anchors[i];
> >                 if (anchor.getAttribute('rel') == 'help') {
> >                         anchor.attachEvent(
> >                                 'onclick',
> >                                 function() {
> >
> window.open(event.srcElement.getAttribute('href'),'popupwindow','width=400,h
> > eight=400,scrollbars=1,resizable=1'); return false; }
> >                         );
> >                 }
> >         }
> > </script>
> > 
> > 
> > > -----Original Message-----
> > > Here's a function:
> > >
> > > function helpLinks()
> > >       {
> > >       if(!document.getElementsByTagName) return;
> > >       var anchors = document.getElementsByTagName("a");
> > >       for (var i=0; i<anchors.length; i++)
> > >               {
> > >               var anchor = anchors[i];
> > >               if (anchor.getAttribute("href") &&
> > anchor.getAttribute("rel")
> > > ==
> > > "help")
> > >                       {
> > >                       anchor.setAttribute(
> > > "onclick","window.open(this.href,'popupwindow','width=400,height=400,scr
> > > ollbars,resizable'); return false;",0);
> > >                       }
> > >               }
> > >       }
> > >
> > > It works perfectly well in everything I can get my hands on except for
> > > IE, where it fails to set the onclick event to all A elements with a
> > > rel attribute of 'help'.
> > >
> > > Changing anchor.setAttribute(...) to
> > > anchor.setAttribute('target','_blank',0); DOES work (the link opens in
> > > a new window), so it would appear that IE doesn't like setting onlick
> > > attributes this way.
> > >
> > > Can anyone either:
> > > - suggest an alternate way to achieve this, or
> > > - suggest a good mailing list to seek further help on (like a DOM list)
> > 
> > ******************************************************
> > The discussion list for  http://webstandardsgroup.org/
> > 
> > Proud presenters of Web Essentials 04 http://we04.com/
> >  Web standards, accessibility, inspiration, knowledge
> > To be held in Sydney, September 30 and October 1, 2004
> > 
> >  See http://webstandardsgroup.org/mail/guidelines.cfm
> >  for some hints on posting to the list & getting help
> > ******************************************************
> > 
> >
> ******************************************************
> The discussion list for  http://webstandardsgroup.org/
> 
> Proud presenters of Web Essentials 04 http://we04.com/
>  Web standards, accessibility, inspiration, knowledge
> To be held in Sydney, September 30 and October 1, 2004
> 
>  See http://webstandardsgroup.org/mail/guidelines.cfm
>  for some hints on posting to the list & getting help
> ******************************************************
> 
> 


-- 
Dan Webb
Web Developer and Internet Consultant
www.danwebb.net
07957 234544
39 Roseberry Gardens, London, N8 8SH
******************************************************
The discussion list for  http://webstandardsgroup.org/

Proud presenters of Web Essentials 04 http://we04.com/
 Web standards, accessibility, inspiration, knowledge
To be held in Sydney, September 30 and October 1, 2004

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

Reply via email to