Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 window pops up correctly. If my IDENTICAL javascript code is called from either the script tag at the beginning of the HTML document or from a separate js file, the link (or image in my case) opens not in a popup, but in the same window. < frustration >

This opens a popup:

<a href="test.jpg" onclick="window.open('test.jpg','test_name','width=500,height=500,scrollbars=no'); return false">test_name</a>


This does not open a popup:

function cleanPopup(url, title) {
window.open(url, title, 'width=500,height=500,scrollbars=no');
return false;
}

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


Any help would be appreciated.

PS: Tested in Chrome and Firefox.

EDIT:

I've discovered my problem. I originally only called the js file in the head tag. There is something about the layers of div's when created with multiple scripts of a templating tool (Template Toolkit in my case) that makes the original script element within the head element seemingly invisible to the deeper child elements.

I do not know exactly what's going on, and I don't have the time to explore it. I've added this edit just in case some other person has a similar issue and somehow stumbles across this thread. If someone understands this phenomenon, and can explain it, please do.

EDIT 2:

The "name" parameter of the window.open method can not contain spaces for the popup to work in IE. Thanks M$.

like image 520
s2cuts Avatar asked Jul 28 '26 01:07

s2cuts


2 Answers

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>
like image 101
mplungjan Avatar answered Jul 29 '26 14:07

mplungjan


use target="_blank"

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

from HTML, or

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

from javascript

like image 22
BiAiB Avatar answered Jul 29 '26 15:07

BiAiB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!