Need to open ALL links in new window

This is a discussion on "Need to open ALL links in new window" within the JavaScript Forum section. This forum, and the thread "Need to open ALL links in new window are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 29th, 2006, 18:51
New Member
Join Date: Jul 2006
Location: London
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question Need to open ALL links in new window

Hi,
I need a javascript code to put in a small page so that ALL of the links there are opened in a new window. This page will be embeddded in an iframe as it is from another host using php.

So all i need to do is add the javascript code to that page. If anyone knows please tell me.

P.s I don't want those ones which open new links from other host's (external sites). I need the one to open all the links.

Thanks
Reply With Quote

  #2 (permalink)  
Old Jul 30th, 2006, 21:45
New Member
Join Date: Jul 2006
Location: Earth
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Need to open ALL links in new window

document.onload = function() {
var frame = parent.frames[0].document;
var links = frame.getElementsByTagName('a');
for (var i=0; i<links.length; ++i) {
links[i].setAttribute('target','_blank');
}
}

Last edited by jtek; Jul 30th, 2006 at 21:53.
Reply With Quote
  #3 (permalink)  
Old Jul 31st, 2006, 12:44
New Member
Join Date: Jul 2006
Location: London
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Need to open ALL links in new window

Quote:
Originally Posted by jtek View Post
document.onload = function() {
var frame = parent.frames[0].document;
var links = frame.getElementsByTagName('a');
for (var i=0; i<links.length; ++i) {
links[i].setAttribute('target','_blank');
}
}
Nope this code doesn't work.
I'm sure there is a solution to this problem.
Here is the javascript code i put in:

<SCRIPT language="JavaScript">
document.onload = function() {
var frame = parent.frames[0].document;
var links = frame.getElementsByTagName('a');
for (var i=0; i<links.length; ++i) {
links[i].setAttribute('target','_blank');
}
}
</SCRIPT>

is this correct.
If anyone knows, pls reply.
Thanks anyway jtek
Reply With Quote
  #4 (permalink)  
Old Jul 31st, 2006, 16:19
New Member
Join Date: Jul 2006
Location: Earth
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Need to open ALL links in new window

Sorry for the previous post. I should have tested the code first. It turns out that document.onload needs to be changed to just onload. I have posted my test pages below.
Code: Select all
//test.htm
<html>
<head>
<script>
onload = function() {
    var frame = parent.frames[0].document;
    var links = frame.getElementsByTagName('a');
    for (var i=0; i<links.length; ++i) {
        links[i].setAttribute('target','_blank');
    }
}
</script>
</head>
<body>
<iframe src="test2.htm" width="200" height="100"></iframe>
</body>
</html>
Code: Select all
//test2.htm
<html>
<head>
</head>
<body>
<a href="test.htm">one</a><br><br>
<a href="test.htm">two</a><br><br>
<a href="test.htm">three</a>
</body>
</html>

Last edited by jtek; Jul 31st, 2006 at 16:21.
Reply With Quote
  #5 (permalink)  
Old Aug 4th, 2006, 18:46
New Member
Join Date: Jul 2006
Location: London
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Need to open ALL links in new window

thanks for you help but what i want to do is embed the code in the iframe only so that all of it's links open in a new window
Reply With Quote
Reply

Tags
open, links, window

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
how can have iframe links to open in a new window? onSPOT Web Page Design 1 Mar 10th, 2008 07:43
Why do links in a frame open a new window? SpecialBrew Web Page Design 8 Jan 25th, 2008 08:50
Change open in new window to open in same window nsr500rossi JavaScript Forum 2 Jan 18th, 2008 14:13
[SOLVED] open new window from main window AdRock Other Programming Languages 1 Nov 1st, 2007 02:45
Need JavaScript for html links should open in a new window in the first time, after.. jayaramgussy JavaScript Forum 0 Jun 19th, 2007 09:00


All times are GMT. The time now is 04:38.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43