Forum Moderators: open

Message Too Old, No Replies

onclick and window.open

How to make links open in a new window

         

dreamer81

7:27 am on Jul 23, 2005 (gmt 0)

10+ Year Member



Hi i have a question. I want to cloak my urls, and so far i have made it this way.

<a href="http://www.domain.com" onclick="location.href='http://www.domain.com/?click=33224';return false;">domain.com</a>

but i want those links to open up in a new window. How do I do that?

j4mes

8:34 am on Jul 23, 2005 (gmt 0)

10+ Year Member



window.open(), or use target="_blank"

Beware though, a lot of popup blockers will catch and disable window.open.

J.

rocknbil

6:02 pm on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am still looking for an instance of a user-generated action, such as a link click, that gets blocked. Pop-ups are usually blocked by an inline window.open or an onLoad function.

You generally want to pass the click to a function (see below) so you can add extented coding:

onClick="newWindow('file.html');"

or pass parameters for a document.write once it gets there, such as just for an image:
onClick="newWindow('image.jpg','Image Title',500,250);"

To insure viewers will see your stuff regardless of whether or not Javascript is enabled, make the actual href to whatever you would open (file.html or image.jpg.) Normally this would cause the page to load the link in the href, but the return false; executes only IF Javascript is enabled, and this tells the browser NOT to follow the link.

<a href="file.html" onClick="newWindow('file.html');" return false;">Click me</a>

or

<a href="image.jpg" onClick="newWindow('image.jpg','Image Title',500,250);" return false;">Click me</a>

The window.open has many parameters, the most important of which are the window name, an object handle by which you reference the window, the url, and all of the size/toolbar parameters.

You can simply load an image or URL into the new window or dynamically write to it. The Date function below assigns a unique window name to the window so it always opens in a new one. If you use "My_window_name" for the name, every click will open in the same window.

For a simple URL:

<script type="text/javascript">
function newWindow(url) {
var day= new Date();
var id = day.getTime();
var win = open(url,id,'width=500,height=500,scrollbars,resizable');
}
</script>

The version below uses document.write to populate the window content, allowing a close button on the page without writing an html file for every image. It opens a new window based on the image size. (If you use large images, you'll have to test the browser for screen area so it all fits on the screen, it is SO ANNOYING for a window to pop up that's larger than your viewport.) Don't forget to close() or you'll get a blank window:

<script type="text/javascript">
function newWindow(img,title,w,h) {
var day= new Date();
var id = day.getTime();
var ww = w+75; // Make room for scrollbars
var wh = h+125; // and a close button
var params = 'width='+ww+',height='+wh+',scrollbars,resizable';

// Build the output and store it all in msg
var msg='<html><head><title>'+img+'</title></head><body><h3 aligh="center">'+
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"><br>\n'+
'<hr width="100%" size="1"></form></div></body></html>\n';

// Write it all out
var win = open('',id,params);
win.document.write(msg);
win.document.close();
}
</script>

nigassma

6:40 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Thanks for the post RocknBill, but I was wondering why return false; isn't doing as you say it should. From what I think I understand you said that it tells the browser to not follow the ahref if it has java enabled. But I've found that it still follows the ahref when java is enabled. Any idea?

nigassma

6:51 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Thanks for the post RocknBill, but I was wondering why return false; isn't doing as you say it should. From what I think I understand you said that it tells the browser to not follow the ahref if it has java enabled. But I've found that it still follows the ahref when java is enabled. Any idea?