Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open new window with javascript

Status
Not open for further replies.

fullmoon2

Technical User
Aug 14, 2005
20
I have some images in each <li><a href="...>
and with the code below I got a swap effect on the images and if you click on them, a new page comes up. But.. the new page shows in the same window. That's the problem!


$("ul.thumb li a").click(function() {
var mainImage = $(this).attr("href");
location.href = mainImage;
return false;
});

I've tried to google "target_blank javascript", and found f.ex
location.href(target = _blank);
or
window.open(this.getAttribute('href'), '_blank');
But both of them opens the new page into a new, and the same window.
Can anyone help?
 
Hi

fullmoon2 said:
But both of them opens the new page into a new, and the same window.
You mean, if the visitor clicks the link 3 times, you want to open 3 windows containing the same document ?

If [tt]window.open()[/tt]'s second parameter is the same, the document will be opened in the window with the same name.

Just use a generated second parameter to get different window each time :
Code:
window[COLOR=darkred].[/color][b][COLOR=black]open[/color][/b][COLOR=darkred]([/color][b][COLOR=blue]this[/color][/b][COLOR=darkred].[/color]href[COLOR=darkred],[/color][COLOR=red]'win'[/color][COLOR=darkred]+[/color][b][COLOR=blue]new[/color][/b] [b][COLOR=black]Date[/color][/b][COLOR=darkred]().[/color][b][COLOR=black]getTime[/color][/b][COLOR=darkred]())[/color]
Actually [tt]window.open()[/tt]'s second parameter is mandatory, but currently all browsers ignore its absence :
Code:
window[COLOR=darkred].[/color][b][COLOR=black]open[/color][/b][COLOR=darkred]([/color][b][COLOR=blue]this[/color][/b][COLOR=darkred].[/color]href[COLOR=darkred])[/color]

Feherke.
 
I'm not sure if I explained it right;
when the visitor clicks (once) the image/link, it will open in a new window.

With your code, it opens in 2 windows (the window with the links and in a new window..)
Not exactly that I wanted..
 
Hi

fullmoon2 said:
With your code, it opens in 2 windows (the window with the links and in a new window..)
I only corrected your code. If you modified something else too, we can not know it if you not post it.

Let me guess. You removed the [tt]return[/tt] statement from the event handler [tt]function[/tt]. It is still required if you want to stop the default behavior of following the clicked link.


Feherke.
 
No, I didn't removed anything.I just putted your code into the function which already was written, like this:

$("ul.thumb li a").click(function() {
var mainImage = $(this).attr("href");
location.href = mainImage;
window.open(this.href,'win'+new Date().getTime());
return false;
});

And now I got two windows with the same page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top