| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Whippet Botherer™
Join Date: Jan 2007
Location: Kent, England
Posts: 1,525
|
open link in new window problem…
Probably something obvious but I can't see it! Trying to open link in new window with JS, here is code, which I think is perfect, taken from very reliable source: function externalLinks() { 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") == "external") anchor.target = "_blank"; } } window.onload = externalLinks; Here is HTML, can you see anything wrong with my placement of the 'rel=external' part (or anything else)?: <h5>This page is valid <span class="second_links"> <a href="test.html" rel="external" >XHTML</a> </span>& <span class="second_links"> <a href="test.html">CSS</a>. </span>© 2008.</h5> Site is not live so can't post link, any help appreciated. Last edited by Tom_E : 14-01-2008 at 18:01. |
|
|
|
|
|
#3 (permalink) |
|
trouble free and loverlee
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
|
Making the change which Hunch suggests shouldn't make any difference, as the 'DOM 0' method - obj.target = '_blank' - is well supported (actually, better supported than the W3C DOM method which he recommends). I don't see anything wrong with the code in your opening post, which is presumably sourced from the SitePoint article. It should work. If it's not working, then I'd say the problem is rooted elsewhere within the document, possibly some malformed markup elsewhere in the document, which is making DOM traversal an issue for this script. Does the entire markup document validate without errors? Which browser(s) is the script is failing in? |
|
|
|
#4 (permalink) |
|
Whippet Botherer™
Join Date: Jan 2007
Location: Kent, England
Posts: 1,525
|
Yep, script is taken from Sitepoint article. The code validates perfectly but link opens in the same window in all browsers on my mac so you are probably right that the problem lies elsewhere in the document. |
|
|
|
#5 (permalink) |
|
Whippet Botherer™
Join Date: Jan 2007
Location: Kent, England
Posts: 1,525
|
Brought the JS internal and it now works. God knows why, it was referenced perfectly, but I'm no expert with JS/DOM so probably something I'd done wrong somewhere. Either way it's working now so happy days! |
|
|
|
#6 (permalink) | |
|
Grumpy old man
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
|
Quote:
I didn't recommend anything. He asked how to make it work. I told him. If his version is better supported, why did a cut and paste of a single line of my code make it work, when his version didn't? Try it in a range of browsers and then tell me which one is more widely accepted instead of relaying something you read on some website. |
|
|
|
|
#7 (permalink) | |||
|
trouble free and loverlee
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
|
Quote:
Good for you. Quote:
Quote:
I've been dealing with js long enough to know the comparative pros and cons of DOM 0 and W3C DOM. |
|||
|
|
|
#8 (permalink) |
|
Whippet Botherer™
Join Date: Jan 2007
Location: Kent, England
Posts: 1,525
|
Hey, calm down guys – the main thing is it's working (the original js script from Sitepoint)! Thank you both for the advice, I'm not going to argue with either of you, you both know far more about it than I do. |
|
|
|
#9 (permalink) | |
|
Grumpy old man
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
|
Quote:
I think you said it most clearly when you said it 'should' work. I prefer to write code that works. Which mine did. It was the easiest and quickest fix to get the job done and was standards compliant. Whether or not you consider it broken is irrelevant. If it doesn't work in a range of commonly used browsers, it isn't worth squat... I apologize for the slightly abrupt nature of my previous post. It was written after a particularly drunken dinner with a business associate. But I stand by my basic assertion. |
|
|
|
|
#10 (permalink) | ||
|
trouble free and loverlee
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
|
Quote:
Like you, I prefer to write code which works, which is why I didn't bother suggesting swapping out the DOM 0 method for the W3C in this case. It wouldn't have made any difference. It, alone, wouldn't have made the script start working, because the problem wasn't with that particular passage of code. Quote:
The problem was not with the portion of code which the OP posted. Try to appreciate that the 'solution' which you posted is fundamentally identical to the OP's original code (c/o SitePoint). Both do work perfectly well for those browsers typically supported today. (The only difference is that the 'DOM 0' method used in the OP's/SitePoint method actually gives better backward compatibility than the W3C method - though that may not be important for the site in question.) I am 100% certain that the problem was rooted elsewhere and not with the js code originally posted. It's admirable that you put forward something and that you seem to know a bit about W3C DOM, but your 'fix' alone was not going to solve the OP's problem. (Tbh, I don't really understand why you find that so hard to grasp, though, tbh, it sounds as though your enthusiasm for standards compliance could be leading you to think of it as a universal panacea and blinkering your approach to debugging. Don't get me wrong, I'm very much pro-W3C, but you also need to know enough about js to appreciate that, in this case, using the W3C method wasn't going to fix the problem.) Still, all's well… It's all good. Last edited by Bill Posters : 15-01-2008 at 03:28. |
||
|
![]() |