Forum Moderators: open
or with java,
<SCRIPT TYPE="text/javascript">
<!--
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=400,height=500,scrollbars=yes');
return false;
}
//-->
</SCRIPT>
The java above goes in head
<a href="http://www.widgets.com/" onClick="return popup(this, 'notes')">
... works as well.
The solution to the first is to use a regular link to the resource, and put the call to the routine in an onClick event and return false after the call. This tells Javascript NOT to actually allow the page to move to the resource. If Javascript is disabled, the inverse will happen and at least they can access your content.
The solution to the second is to make sure windowname is a unique value. This feature is far underused. By basing the new window name (which is really a window ID) on a value we get from the current time, a new window will open EVERY time the link is clicked.
Additionally you can structure your link so it can pass an image OR a URL, and the parameters (width, height, etc) do not need to be specified as true or false, yes or no. Include them if you want them, leave them out if you don't, as in
scrollbars,resizable
instead of
scrollbars=yes,resizable=yes
And always ALWAYS include the resizable parameter, there are many other worlds than these.* :-)
<!--
pass size OR image, a window size, and a TITLE (not window name.)
Note in the first the param for image is blank,
in the second the param for url is blank.
If the resource is an image, pass the size of the large image - the window will be padded by the JS
-->
<a href="myurl.html" onClick="newWin('my_url.html','','My Title',500,500); return false;">My URL</a> <br>
<a href="my_image.jpg" onClick="newWin('','my_image.jpg','Title of my Image',600,450); return false;"><img src="my_image_thumbnail.jpg" width="150" height="75" border="0" alt="Click to see enlargement"></a> <br>
<script language="text/javascript">
function newWin(url,img,title,w,h) {
if (! (url) &&! (img)) { return; } // squelch errors
// Create a unique window name based on client time
var day= new Date();
var id = day.getTime();
// Add some space from the passed w and h parameters
var ww = w+75;
var wh = h+125;
// You will get less errors by putting the params
// in a variable, like so
var params = 'width='+ww+',height='+wh+',scrollbars,resizable';
// A little flexibility: this can be used to pass
// either a plain image or a url. If a plain image,
// build a little document to hold it in:
if (img!='') {
var msg='<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN" "http:\/\/www.w3.org/TR\/html4\/loose.dtd">\n'+
'<meta http-equiv="Content-Type" content="text\/html; charset=iso-8859-1">\n'+
'<html><head><title>'+img+'<\/title><\/head>'+
'<style type="text\/css">\n'+
' html,body { font-family: Arial,Helvetica,Sans-Serif; text-align: center; }\n'+
'<\/style><body>\n'+
'<h3>'+title+'<\/h3>\n <div align="center"><img src="'+img+'" width="'+w+'" height="'+h+'" border="0" alt="'+title+'"><br>\n'+
<hr width="100%" size="1"><form><input type="button" onClick="javascript:window.close();" value="Close Window">\n'+
'<hr width="100%" size="1"><\/form><\/div><\/body><\/html>\n';
var win = open('',id,params);
win.document.write(msg);
win.document.close();
}
else { var win = open(url,id,params); }
}
</script>
*The Gunslinger, Dark Tower Series
Note the W3C recommendation for for target="_blank"
The user agent should load the designated document in a new, unnamed window.[w3.org...]
If you use target="new" -- or any other name that's not one of the four reserved target names -- then the browser will load the url into a window with that name. If such a window already exists, then its content gets replaced. If such a window does not exist, a new window will be opened for the url in the anchor and given the name that the target attribute designates.
So you can get a new window with target="new" or target="_new" but it is a NAMED window. If you open the new window with target="blank" it will be an UNNAMED window.