PDA

View Full Version : Random link button - opening link in a new window


olsza7
05-14-2009, 08:31 PM
Hello,
I am using the following script (see below) to place a button on my page that generates several random links. It works very well but I would like the links to open in a new window (right now, it opens in the same window). What do I need to change in this script to achieve that? Thanks so much!


(part one of the script):

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function go_to(url) {
window.location=url;
}
function rand_link() {
var a;
a = 1+Math.round(Math.random()*3); // a = random number between 1-3
if (a==1) go_to("http://bgeffects.javascriptsource.com");
if (a==2) go_to("http://clocks.javascriptsource.com");
if (a==3) go_to("http://games.javascriptsource.com");
}
// End -->
</SCRIPT>

(part 2 of the script):

<BODY>
<CENTER>
<FORM NAME="myForm">
<INPUT TYPE="button" NAME="myButton" VALUE="Random link"
onClick="rand_link()">
</FORM>
</CENTER>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

adios
05-14-2009, 10:06 PM
Just google 'javascript open window' and find yourself a nice window opening script. Let's say it's called 'popUp(url)' where 'url' gets passed the ... um ... url. Then just go

function go_to(url) {
popUp(url);
}

Follow any other instructions for that function.
Setting window.location opens the url in the current window.

olsza7
05-14-2009, 10:50 PM
Thank you Adios, this worked :)

So what I did was substitute 'window.location=url' with 'window.open(url)' in the script above and now the link opens in a new window in IE and in a new tab in Firefox.

adios
05-14-2009, 10:56 PM
Great. Just fyi: you can control the appearance, placement, etc. with window.open. Here's a nice opener:

http://www.pageresource.com/jscript/jwinopen.htm

tyre
05-15-2009, 09:29 AM
http://www.trade9shoes.com :thumbsup: