Opening a new tab in JS
Is it po开发者_如何学Gossible to open a new tab in JS?? I tried googling it but most answers were answered before about years that it was not possible then, so is it possible now?!
Thank in advance :))
another question if possible, can we open to separate Urls using the same tag? I mean to open 2 diff tabs when clicking on one hyperlink
Yeah. You could do this in Javascript using window.open('target.html','mywindow','width=400,height=200')
added to the onclick event.
See this for more info.
Opening a new tab in modern browsers is the same as opening a link in a new window by setting the target attribute. With HTML you do this by:
<a href="url" target="_blank">Click me</a>
(See HTML a target attribute)
With javascript you do the same by window.open(url, '_blank'...) but remember that most browsers will block this unless it is done on the onClick event, so opening a new tab automatically after some timer has gone off for example is not a good idea.
You can also use <a href="/target" target="_blank">link text</a>
if it's a link that should open in a new tab.
If you wanted to open both pages at the same time you could always combine both of the techniques mentioned above.
<a href="thispage.html" target="_blank" onclick="window.open('thatpage.html');">Double Link</a>
This will open the pages thispage.html
and thatpage.html
simultaneously.
dont forget to add the title
attribute to your link as opening new windows/tabs without warning users is generally frowned upon. Something like;
title="Clicking this link will open two new tabs"
should keep the standardistas off your back.
Also, you may want to separate your onclick
event from your html
as again munging them all together really isnt best practice. If you are using jquery then assign the onclick
event by inserting a small piece of JavaScript at the top of your page as such;
$(function(){
$('#the-link-id').click(function(){
window.open('thatpage.html');
});
);
精彩评论