Skip to content Skip to sidebar Skip to footer

Javascript's Window.open Function Inconsistent, Does Not Open Pop Up When Expected

Ok, I'm going nuts here. I'm trying to implement a basic popup window to display images. If my javascript code is fully specified in an HTML tag with the onclick property, the wi

Solution 1:

DEMO HERE

This code is the closest to foolproof you can get in my opinion.

Tested on

  • Windows - Fx, Chrome, IE8 and Safari
  • iPhone: Mobile Safari
  1. adding a target makes the link open in a new window or tab if allowed - in case the script fails for any reason - this is a SIDE-EFFECT which is useful but not the answer to the question.
  2. returning true if the window.open fails will also make the click follow the link, hopefully invoking the target - Note that some browsers no longer reacts to target.
  3. the height (and width) in the 3rd parameters will enforce the opening in a new window rather than a new tab unless the browser is set to open all new windows in a tab
<html>
<head>
<script type="text/javascript">
window.onload=function() {
 document.getElementById('popup').onclick=function() {
   var w = window.open(this.href, this.target, 
       'width=500,height=500,scrollbars=no');
    return (!w); // opens in new window/tab if allowed
  }
}
</script>
</head>
<body>

<a id="popup" href="test.jpg" target="test_name">test_name</a>
</body>
</html>

Solution 2:

use target="_blank"

<a href="whatever" target="_blank"> ...

from HTML, or

window.open(url, '_blank', options)

from javascript


Solution 3:

Make new window title random

onclick="PopupCenter(this.href,'random','600','700','yes'); return false;"

and in function:

if(title=='random')
{
title = randomID();
}

Solution 4:

try this

function mypopup()
{
    mywindow = window.open("http://www.test.com", "mywindow", "location=1,status=1,scrollbars=1,  width=100,height=100");
}

Solution 5:

Make sure you put your javascript code in the Head block

<head>
<script type="text/javascript" language="JavaScript">
function cleanPopup(url, title) {
    window.open(url, title, 'width=500,height=500,scrollbars=no');
    return false;
}
</script>

And in the body:

<a href="" onclick="cleanPopup('test.jpg','test_name')">test_name</a>

It just work for me without any problem both in Firefox and IE


Post a Comment for "Javascript's Window.open Function Inconsistent, Does Not Open Pop Up When Expected"