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><scripttype="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><aid="popup"href="test.jpg"target="test_name">test_name</a></body></html>

Solution 2:

use target="_blank"

<ahref="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

functionmypopup()
{
    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><scripttype="text/javascript"language="JavaScript">functioncleanPopup(url, title) {
    window.open(url, title, 'width=500,height=500,scrollbars=no');
    returnfalse;
}
</script>

And in the body:

<ahref=""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"