Re: [WSG] DOM setAttribute in IE?
Thanks to everyone for their help -- a few great solutions in no time at all. I finally stumbled on this via google, and it looks the goods so far: anchor.onclick = function() { ... } Since others have also recommended it, I'm pretty confident in it's use. And you're all right, we should be avoiding IE proprietary methods at all times (this is why we're on a standards list!) --- Justin French http://indent.com.au ** 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 **
Re: [WSG] DOM setAttribute in IE?
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: > var anchors = document.getElementsByTagName('a'); > for (var i=0; iif (anchors[i].rel == 'help') anchors[i].onclick = function(){ window.open(this.href,'popupwindow','width=400,height=400,scrollbars=1,resizable=1'); return false; }; > } > 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. > > > > -- > > test > > > > > > var anchors = document.getElementsByTagName('a'); > > for (var i=0; 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; } > > ); > > } > > } > > > > > > > > > -Original Message- > > > Here's a function: > > > > > > function helpLinks() > > > { > > > if(!document.getElementsByTagName) return; > > > var anchors = document.getElementsByTagName("a"); > > > for (var i=0; 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://w
RE: [WSG] DOM setAttribute in IE?
I'd suggest using Scott Andrews' addEvent helper function (see http://www.scottandrew.com/weblog/articles/cbs-events) function addEvent(obj, evType, fn, useCapture){ if (obj.addEventListener){ obj.addEventListener(evType, fn, useCapture); return true; } else if (obj.attachEvent){ var r = obj.attachEvent("on"+evType, fn); return r; } else { alert("Handler could not be attached"); } } This has worked for me quite consistently in the past. Patrick Patrick H. Lauke Webmaster / University of Salford http://www.salford.ac.uk ** 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 **
RE: [WSG] DOM setAttribute in IE?
IE does not like setAttribute onClick. I had a similar problem recently. I solved it by using the correct method first (as yours) then adding a function to deal with IE after it. function onclickIE(idAttr,handler,call){ if ((document.all)&&(document.getElementById)){idAttr[handler]=new Function(call)} } Hope that helps mike 2k:)2 mike foskett [EMAIL PROTECTED] http://www.webSemantics.co.uk -Original Message- From: Justin French [mailto:[EMAIL PROTECTED] Sent: 19 August 2004 07:22 To: [EMAIL PROTECTED] Subject: [WSG] DOM setAttribute in IE? Here's a function: function helpLinks() { if(!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; ihttp://indent.com.au ** 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 ** ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com ** ** 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 **
Re: [WSG] DOM setAttribute in IE?
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. > > -- > test > > > var anchors = document.getElementsByTagName('a'); > for (var i=0; ivar 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; } > ); > } > } > > > > > -Original Message- > > Here's a function: > > > > function helpLinks() > > { > > if(!document.getElementsByTagName) return; > > var anchors = document.getElementsByTagName("a"); > > for (var i=0; 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 **
RE: [WSG] DOM setAttribute in IE?
You can use attachEvent method for this. But it's supported by Internet Explorer 6 only. -- test var anchors = document.getElementsByTagName('a'); for (var i=0; i-Original Message- > Here's a function: > > function helpLinks() > { > if(!document.getElementsByTagName) return; > var anchors = document.getElementsByTagName("a"); > for (var i=0; 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 **
Re: [WSG] DOM setAttribute in IE?
On Thu, 2004-08-19 at 16:22, Justin French wrote: > Can anyone either: > - suggest an alternate way to achieve this, or This might help http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/attachevent.asp if (anchor.attachEvent) anchor.attachEvent("onClick", ); ** 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 **
[WSG] DOM setAttribute in IE?
Here's a function: function helpLinks() { if(!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; 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) --- Justin French http://indent.com.au ** 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 **