Reply LinkBack Thread Tools Search this Thread
Old 13-01-2008, 19:57   #1 (permalink)
Tom_E
Whippet Botherer™
 
Tom_E's Avatar
 
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>&amp; <span class="second_links"> <a href="test.html">CSS</a>. </span>&copy; 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.
  Reply With Quote
Old 13-01-2008, 21:51   #2 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
Change the line:

Code:
anchor.target = "_blank";

to:

Code:
anchor.setAttribute('target', '_blank');

and it will work.
  Reply With Quote
Old 14-01-2008, 03:03   #3 (permalink)
Bill Posters
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?
  Reply With Quote
Old 14-01-2008, 06:04   #4 (permalink)
Tom_E
Whippet Botherer™
 
Tom_E's Avatar
 
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.
__________________
  Reply With Quote
Old 14-01-2008, 06:49   #5 (permalink)
Tom_E
Whippet Botherer™
 
Tom_E's Avatar
 
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!
__________________
  Reply With Quote
Old 14-01-2008, 10:07   #6 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
Quote:
Originally Posted by Bill Posters
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 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.
  Reply With Quote
Old 14-01-2008, 13:34   #7 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Quote:
Originally Posted by Hunch
I didn't recommend anything. He asked how to make it work. I told him.
You put forward a fix for a part of the script which wasn't actually broken.
Good for you.

Quote:
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?
Before I posted, I pasted the code which s/he posted to check it myself. It worked perfectly - as I expected.

Quote:
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.
Shut up, you pillock.
I've been dealing with js long enough to know the comparative pros and cons of DOM 0 and W3C DOM.
  Reply With Quote
Old 14-01-2008, 14:10   #8 (permalink)
Tom_E
Whippet Botherer™
 
Tom_E's Avatar
 
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.
__________________
  Reply With Quote
Old 14-01-2008, 17:47   #9 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
Quote:
Originally Posted by Bill Posters
You put forward a fix for a part of the script which wasn't actually broken.

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.
  Reply With Quote
Old 15-01-2008, 03:13   #10 (permalink)
Bill Posters
trouble free and loverlee
 
Join Date: Mar 2003
Location: YooKay
Posts: 2,968
Quote:
Originally Posted by Hunch
I think you said it most clearly when you said it 'should' work. I prefer to write code that works. Which mine did.
The code the OP already had, at least, the part which s/he posted - worked.
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:
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...
The point of debugging is to track down problems and fix them, not swap other, irrelevant parts of the code.
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.
  Reply With Quote
Old 15-01-2008, 03:47   #11 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
Edited: There's no point in carrying on this shit.

Last edited by Hunch : 15-01-2008 at 04:40.
  Reply With Quote
Old 15-01-2008, 04:41   #12 (permalink)
Jayx
Now with added sarcasm.
 
Jayx's Avatar
 
Join Date: Aug 2006
Location: George, South Africa
Posts: 351
Send a message via MSN to Jayx Send a message via Skype™ to Jayx
Edit: nothing to see, move along now.
__________________
I made 100 posts and all I got is this stupid signature.

Last edited by Jayx : 15-01-2008 at 05:15.
  Reply With Quote
Old 15-01-2008, 04:42   #13 (permalink)
Hunch
Grumpy old man
 
Hunch's Avatar
 
Join Date: Oct 2007
Location: North Japan
Posts: 1,672
Show's over
  Reply With Quote
Old 15-01-2008, 04:44   #14 (permalink)
Coops
Pops.
 
Coops's Avatar
 
Join Date: Aug 2007
Posts: 3,603
Tedious.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8