...

View Full Version : Open a New Window



hiyatran
08-21-2011, 02:13 PM
when a user click on a link, it would open a new window in HTML it is something like this:

<a href="http://google.com" target="_blank">click here</a>


How do I do this in Javascript? So a user would click on the link and it would open a new window. Here's my code

document.writeln('<tr><td>' + "http://google.com" + '</td></tr>');

I know window.open(url) would open a new window, but how do I make it clickable??

Any comments or suggestion would be greatly appreciated.
thanks

Philip M
08-21-2011, 03:51 PM
http://www.pageresource.com/jscript/jwinopen.htm

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

webdev1958
08-21-2011, 04:00 PM
I know window.open(url) would open a new window, but how do I make it clickable??


Is this what you mean?


<a href="" onclick="window.open('someURL'); return false;">Click to open new window</a>

you can add extra parameters to window.open to size the window etc etc etc

hiyatran
08-21-2011, 07:02 PM
I would like to put the link in a javascript and NOT HTML, like so

document.writeln('<tr><td onclick="window.open('http://www.google.com');">Click Here</td></tr>');

but doesn't work!!

Philip M
08-21-2011, 07:30 PM
I would like to put the link in a javascript and NOT HTML, like so

document.writeln('<tr><td onclick="window.open('http://www.google.com');">Click Here</td></tr>');

but doesn't work!!

Why? What is the point?


<table>
<script type = "text/javascript">
document.writeln('<tr><td> <a href = "" onclick="window.open(\'http://www.google.com\');">Click Here</td></tr></a>');
</script>
</table>


Note the document.writeln that runs after the page finishes loading will create a new page and overwrite all of the content of the current page.

webdev1958
08-22-2011, 12:59 AM
@op - it better to use dom methods (createElement(),appendChild() etc) to create elements and assign event handler functions rather than document.write or innerHTML as some people also do.




document.writeln('<tr><td> <a href = "" onclick="window.open(\'http://www.google.com\'); return false;">Click Here</td></tr></a>');



If you don't want to lose the contents of the parent window due to the blank href you will need to add return false; to cancel the default action of the href.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum