views:

44

answers:

3

Hi all,

I am trying to open a location in new window(tab) using window.open. It is not working in chrome. First I tried with window.open(url,name), this did not work, however this works in every other browser. Then I used something like this,

var w = window.open("about:blank");
w.opener = null;
w.document.location = url;

This opens the url in same tab but not in separate tab.

Please help.

A: 

Do it like this

window.open( url, "_blank" );

Remember, the 2nd parameter is analogous to an anchor tag's target attribute.

Peter Bailey
Peter, this is not working :(. I am not using anchor tag, its a flash, when clicked, the url has to open in new tab.
Harsha
Chrome is probably blocking it, then.
Peter Bailey
+1  A: 

Are you sure your popup is not being blocked? Most popup windows that didn't happen in response to a user event will get blocked. I typed window.open("google.com", "_blank") into the console and I got the blocked window on the url bar

Juan Mendes
A: 

Try this. Works in IE8, fails in FF when popups are blocked

<html>
<head>
<script type="text/javascript">
if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){ // event by Jason Karl Davis
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}
function loadAndClick(url,target) {
  var lnk = document.createElement("a");
  lnk.href=url;
  lnk.target=target||"_blank"
  lnk.id="myLink"
  lnk.onclick=function() {
    var w = window.open(this.href,this.target);
    return (w)?false:true;
  }
  document.body.appendChild(lnk);
  document.getElementById('myLink').click();
//  lnk.click();
}
window.onload=function() { // or call getURL("javascript:loadAndClick('http://www.google.com')");
  loadAndClick("http://www.google.com");
}  
</script>
</head>
<body>
</body>
</html>
mplungjan