Re: [WSG] Javascript problem
Thanks for your replies everyone, much appreciated. All working well now. Cheers Paul On 21/06/07, Thierry Koblentz <[EMAIL PROTECTED]> wrote: > On Behalf Of Paul Collins > I have a script that adds colours to a all the columns in a table. It > works fine, the only problem is, it is trying to apply the code to all > pages, when the table is only on a couple. So when I am viewing all > other pages, it comes up with this error: > > document.getElementById(tableID) has no properties > > So, what I would like to do, is add a checker to the script to see if > the table actually exists before doing the rest of the code. > Unfortunately, I am a novice to this and I've been stuffing around for > a while and can't get it to work. > > Here is the teh script, it is worth mentioning that this is the only > table on the site, so that may help with the re-working of the code, > although it would be nice to have a checker that looks for the > specific table id. > > Thanks in advance: > > // script to add alternating table background colours > var > colors=["#E5D9DB","#C5D3D8","#DBCBBE","#E9DBC7","#D4E0E0","#C5CEC7"]; > function alternateRows(tableID,numberOfColors,colorArray){ > var > trs=document.getElementById(tableID).getElementsByTagName("TD"); > len=trs.length; > var myColors=colorArray.slice(0,numberOfColors); > while(len--){ > > trs[len].style.backgroundColor=colors[len%myColors.length]; > } > } > > // add onload event > addLoadEvent(function() { > alternateRows("caseStudiesTable",6,colors); > } > ); Try this: addLoadEvent(function() { if(document.getElementById("caseStudiesTable")) alternateRows("caseStudiesTable",6,colors); } --- Regards, Thierry | www.TJKDesign.com *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] *** *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] ***
RE: [WSG] Javascript problem
> On Behalf Of Paul Collins > I have a script that adds colours to a all the columns in a table. It > works fine, the only problem is, it is trying to apply the code to all > pages, when the table is only on a couple. So when I am viewing all > other pages, it comes up with this error: > > document.getElementById(tableID) has no properties > > So, what I would like to do, is add a checker to the script to see if > the table actually exists before doing the rest of the code. > Unfortunately, I am a novice to this and I've been stuffing around for > a while and can't get it to work. > > Here is the teh script, it is worth mentioning that this is the only > table on the site, so that may help with the re-working of the code, > although it would be nice to have a checker that looks for the > specific table id. > > Thanks in advance: > > // script to add alternating table background colours > var > colors=["#E5D9DB","#C5D3D8","#DBCBBE","#E9DBC7","#D4E0E0","#C5CEC7"]; > function alternateRows(tableID,numberOfColors,colorArray){ > var > trs=document.getElementById(tableID).getElementsByTagName("TD"); > len=trs.length; > var myColors=colorArray.slice(0,numberOfColors); > while(len--){ > > trs[len].style.backgroundColor=colors[len%myColors.length]; > } > } > > // add onload event > addLoadEvent(function() { > alternateRows("caseStudiesTable",6,colors); > } > ); Try this: addLoadEvent(function() { if(document.getElementById("caseStudiesTable")) alternateRows("caseStudiesTable",6,colors); } --- Regards, Thierry | www.TJKDesign.com *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] ***
Re: [WSG] Javascript problem
On 6/21/07, Dan Dorman <[EMAIL PROTECTED]> wrote: function alternateRows(tableID,numberOfColors,colorArray){ [snipped] if (table) { var trs=document.getElementById(tableID).getElementsByTagName("TD"); [snipped] } } } Whoops! I got a little careless. The line starting with "var trs...", while it will still work, is needlessly verbose, since you've already got the results of getElementById. You can rewrite that line as: var trs = table.getElementsByTagName("TD") to save a few processor cycles and a few keystrokes. Dan Dorman *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] ***
Re: [WSG] Javascript problem
On 21/06/07, Paul Collins <[EMAIL PROTECTED]> wrote: So, what I would like to do, is add a checker to the script to see if the table actually exists before doing the rest of the code. Unfortunately, I am a novice to this and I've been stuffing around for a while and can't get it to work. Simple solution - add this line to the start of your function: if (!document.getElementById(tableID)) return; -- Matthew Pennell // m: 07904 432123 // www.thewatchmakerproject.com *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] ***
RE: [WSG] Javascript problem
Try the following. It will check to see if the element exists before executing the alternateRows() function. addLoadEvent(function() { if(document.getElementById("caseStudiesTable")) { alternateRows("caseStudiesTable",6,colors); } }); -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul Collins Sent: Thursday, June 21, 2007 1:57 PM To: wsg@webstandardsgroup.org Subject: [WSG] Javascript problem Hi all, I hope this is on topic, please ignore it if not, I have a small Jscript problem that shouldn't be hard to sort out, but I am not great with these things... I have a script that adds colours to a all the columns in a table. It works fine, the only problem is, it is trying to apply the code to all pages, when the table is only on a couple. So when I am viewing all other pages, it comes up with this error: document.getElementById(tableID) has no properties So, what I would like to do, is add a checker to the script to see if the table actually exists before doing the rest of the code. Unfortunately, I am a novice to this and I've been stuffing around for a while and can't get it to work. Here is the teh script, it is worth mentioning that this is the only table on the site, so that may help with the re-working of the code, although it would be nice to have a checker that looks for the specific table id. Thanks in advance: // script to add alternating table background colours var colors=["#E5D9DB","#C5D3D8","#DBCBBE","#E9DBC7","#D4E0E0","#C5CEC7"]; function alternateRows(tableID,numberOfColors,colorArray){ var trs=document.getElementById(tableID).getElementsByTagName("TD"); len=trs.length; var myColors=colorArray.slice(0,numberOfColors); while(len--){ trs[len].style.backgroundColor=colors[len%myColors.length]; } } // add onload event addLoadEvent(function() { alternateRows("caseStudiesTable",6,colors); } ); *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] *** ** NOTICE: The information contained in this message is intended for the addressess(s) only and may be confidential, proprietary, or legally privileged. If you have received this message in error or there are any problems with the transmission, please immediately notify us by return e-mail. The unauthorized use, disclosure, copying, or alteration of this message is strictly forbidden. The sender will not be liable for any damages arising from alteration of the contents of this message by a third-party or as a result of any virus being transmitted. This notice is automatically appended to each e-mail message transmitted from the sender's e-mail domain. *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] ***
Re: [WSG] Javascript problem
On 6/21/07, Paul Collins <[EMAIL PROTECTED]> wrote: I hope this is on topic, please ignore it if not, I have a small Jscript problem that shouldn't be hard to sort out, but I am not great with these things... I'm assuming from the subject that you're actually referring to Javascript. Jscript is a similar implementation by Microsoft that's just different enough to screw things up. So when I am viewing all other pages, it comes up with this error: document.getElementById(tableID) has no properties Since there isn't an element with an id attribute equal to tableID, getElementById returns null. However, in your script: var trs=document.getElementById(tableID).getElementsByTagName("TD"); ... you're using the method getElementsByTagName of the element object returned by getElementById. This is fine, as long as getElementById returns something, as it does when the table is present on the page. However, when the table isn't there, getElementById returns null ... and null doesn't have any properties (or methods), hence the error. You're basically saying: var trs = null.getElementsByTagName("TD") However, it's a simple fix: run getElementById on its own, check that it returned something, then run getElementsByTagName on the element which you now know exists. Something like so: function alternateRows(tableID,numberOfColors,colorArray){ var table = document.getElementById(tableID); if (table) { var trs=document.getElementById(tableID).getElementsByTagName("TD"); len=trs.length; var myColors=colorArray.slice(0,numberOfColors); while(len--){ trs[len].style.backgroundColor=colors[len%myColors.length]; } } } The stuff inside the if statement won't execute unless table exists. Hope that helps! Dan Dorman *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] ***
[WSG] Javascript problem
Hi all, I hope this is on topic, please ignore it if not, I have a small Jscript problem that shouldn't be hard to sort out, but I am not great with these things... I have a script that adds colours to a all the columns in a table. It works fine, the only problem is, it is trying to apply the code to all pages, when the table is only on a couple. So when I am viewing all other pages, it comes up with this error: document.getElementById(tableID) has no properties So, what I would like to do, is add a checker to the script to see if the table actually exists before doing the rest of the code. Unfortunately, I am a novice to this and I've been stuffing around for a while and can't get it to work. Here is the teh script, it is worth mentioning that this is the only table on the site, so that may help with the re-working of the code, although it would be nice to have a checker that looks for the specific table id. Thanks in advance: // script to add alternating table background colours var colors=["#E5D9DB","#C5D3D8","#DBCBBE","#E9DBC7","#D4E0E0","#C5CEC7"]; function alternateRows(tableID,numberOfColors,colorArray){ var trs=document.getElementById(tableID).getElementsByTagName("TD"); len=trs.length; var myColors=colorArray.slice(0,numberOfColors); while(len--){ trs[len].style.backgroundColor=colors[len%myColors.length]; } } // add onload event addLoadEvent(function() { alternateRows("caseStudiesTable",6,colors); } ); *** List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm Help: [EMAIL PROTECTED] ***
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
Don't popup blockers allow them when they're a result of a direct user interaction Sometimes, sometimes not. It all depends on browser and/or setup... Or is this another thing that XP SP2 messes up? I'd guess so... -- Jan Brasna aka JohnyB :: www.alphanumeric.cz | www.janbrasna.com ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
Jan Brasna wrote: javascript may fail to open a new window? Popup blockers etc. Don't popup blockers allow them when they're a result of a direct user interaction (such as clicking a link)? Or is this another thing that XP SP2 messes up? -- Patrick H. Lauke _ re·dux (adj.): brought back; returned. used postpositively [latin : re-, re- + dux, leader; see duke.] www.splintered.co.uk | www.photographia.co.uk http://redux.deviantart.com ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
RE: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
Patrick H. Lauke wrote: > so what advantage does this bring? If javascript is enabled, but fails > to open a window, the link still works? What are the situations in which > javascript may fail to open a new window? > Jan Brasna wrote: > > Do not return false. Return !window.open(...) instead. Directive override during biosphere external sensor discrepancy :o) Mike Pepper Accessible Web Developer Internet SEO and Marketing Analyst http://www.seowebsitepromotion.com Administrator Guild of Accessible Web Designers [EMAIL PROTECTED] http://www.gawds.org ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
javascript may fail to open a new window? Popup blockers etc. -- Jan Brasna aka JohnyB :: www.alphanumeric.cz | www.janbrasna.com ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
Jan Brasna wrote: Do not return false. Return !window.open(...) instead. so what advantage does this bring? If javascript is enabled, but fails to open a window, the link still works? What are the situations in which javascript may fail to open a new window? -- Patrick H. Lauke _ re·dux (adj.): brought back; returned. used postpositively [latin : re-, re- + dux, leader; see duke.] www.splintered.co.uk | www.photographia.co.uk http://redux.deviantart.com ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
return false; Do not return false. Return !window.open(...) instead. -- Jan Brasna aka JohnyB :: www.alphanumeric.cz | www.janbrasna.com ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
guys, thanks a lot for helping out :^) -- Thorsten ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
and don't use inline scripts. They are as bad as inline styles: Barkow-Lewinsky, Eva var links = document.getElementsByTagName('a'); for(i=0;i This would be better, in case you have multiple classes assigned to your a tag: if (links[i].className.match(/\bpopup\b/)) Better than class, though, would be to use rel="external" or rel="dialogue" or some such thing. The rel attribute describes the relationship of the destination document to the origin document. Class is a style, and not a relationship. But honestly, 6 of one, half dozen of the other. -- Ben Curtis : webwright bivia : a personal web studio http://www.bivia.com v: (818) 507-6613 ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re: Re[2]: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
On Fri, 11 Mar 2005 18:22:25 -, Chris Dawes <[EMAIL PROTECTED]> wrote: Never put URLs in onclick (javascript:blabla *is* URL) Forget that use this idea: That is tweaked non-standard accessibility killer. It will break even if I middle-click link in a normal browser... In my previous post I've given solution that degrades nicely and requires even less code. -- regards, Kornel Lesiński ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
Re[2]: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
> Never put URLs in onclick (_javascript_:blabla *is* URL) Forget that use this idea: if you have to open more than one window use a function to call the open window functions one after the other: openWindow(params) openWindow(params) hmmm... might be better than returning false from a function that is in fact true Happy coding... Dawesi
Re: [WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
On Fri, 11 Mar 2005 11:49:57 -, Thorsten <[EMAIL PROTECTED]> wrote: Barkow-Lewinsky, Eva Never put URLs in onclick (javascript:blabla *is* URL) Don't use href="#". Put meaningful link there, and avoid duplication by using this.href. Second attribute for window.open is window name and works like target for . To allow more than one pop-up, use _blank. onclick may return false to prevent following href. Barkow-Lewinsky, Eva and don't use inline scripts. They are as bad as inline styles: Barkow-Lewinsky, Eva var links = document.getElementsByTagName('a'); for(i=0;ivoila! -- regards, Kornel Lesiński ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **
[WSG] javascript problem with pop-up on hyperlink click (IE6 displays an error, FF, Opera work fine)
hiya, i'm not sure if this is the right place to pose my question, but i'll dare it. if my question is unwanted on the list, i apologise, maybe someone can reply in private? i have a page from which i'd like to pop-up a window with more details upon clicking a hyperlink. the javascript i'm using is: Barkow-Lewinsky, Eva this works fine in the Fox and Opera, but IE6 says i've got an "invalid argument" in my javascript in the line where the link is. besides asking for advice on how to fix this, i'd be glad to hear alternative approaches, too. thanks, -- Thorsten ** The discussion list for http://webstandardsgroup.org/ See http://webstandardsgroup.org/mail/guidelines.cfm for some hints on posting to the list & getting help **