Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use target in location.href

I am currently developing a web application where I need to open a popup window to show a report. The problem is that some versions of explorer don't support the window.open javascript function, so when this is the case I catch the error and open the new url with location.href. Here the code:

try {
    window.open(url, "","width=1002,height=700,location=0,menubar=0,scrollbars=1,status=1,resizable=0")
} catch(e) {
    location.target = "_blank";
    location.href = url;
}

The problem is that the location.target is not working and I would like to know if there is a way to specify the target of the location.href so it can be opened in a new tab.

like image 328
user1084509 Avatar asked Jan 20 '12 16:01

user1084509


2 Answers

try this one, which simulates a click on an anchor.

var a = document.createElement('a');
a.href='http://www.google.com';
a.target = '_blank';
document.body.appendChild(a);
a.click();
like image 110
qiao Avatar answered Sep 18 '22 20:09

qiao


You can use this on any element where onclick works:

onclick="window.open('some.htm','_blank');"

like image 22
jojo Avatar answered Sep 18 '22 20:09

jojo