/    Sign up×
Community /Pin to ProfileBookmark

Trying to change pop up

Hello,

I have a code that works great for clicking a button and getting a random pop up page, I am trying to adjust the code to change it from a pop up page to have it go to random pages within the current page on click, any suggestions?

Code:

<!– Begin
function go_to(url) {
window.open(url,””, “height=670,width=550”);
}
function rand_link() {
var a;
a = 1+Math.round(Math.random()*3); // a = random number between 1-3
if (a==1) go_to(“http://www.site1.html“);
if (a==2) go_to(“http://www.site2.html“);
if (a==3) go_to(“http://www.site3.html“);
}
// End –>

thanks, Dennis

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@LeeUDec 19.2007 — I'm not sure what you mean by "random pages [b]within[/b] the current page".
Copy linkTweet thisAlerts:
@mona979authorDec 19.2007 — Yes, I mean that when you click on the button it will take you to a random page, but will do it without opening a new window, does this make sense?

thanks, Dennis?
Copy linkTweet thisAlerts:
@LeeUDec 19.2007 — No problem. Just drop the first function and replace the second one with this:
[CODE]
function rand_link() {
var a;
a = 1+Math.round(Math.random()*3); // a = random number between 1-3
if (a==1) window.location = "http://www.site1.html";
if (a==2) window.location = "http://www.site2.html";
if (a==3) window.location = "http://www.site3.html";
}[/CODE]

Understand, though, that some people may have their browsers set to open a new window/tab, which you can't control.
Copy linkTweet thisAlerts:
@magentaplacentaDec 19.2007 — <script type="text/javascript">

var randomlinks=new Array();

randomlinks[0]="http://www.yahoo.com";

randomlinks[1]="http://www.google.com";

randomlinks[2]="http://www.webdeveloper.com";

randomlinks[3]="http://www.sitepoint.com";

randomlinks[4]="http://www.cnn.com";

function randomlink() {

window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]

}

window.onload=randomlink();

</script>
Copy linkTweet thisAlerts:
@LeeUDec 19.2007 — Ah, much nicer. Thanks!
Copy linkTweet thisAlerts:
@mona979authorDec 19.2007 — This looks good, but does it work for a page load or a button click? I need the random url to work with a button.

thanks




<script type="text/javascript">

var randomlinks=new Array();

randomlinks[0]="http://www.yahoo.com";

randomlinks[1]="http://www.google.com";

randomlinks[2]="http://www.webdeveloper.com";

randomlinks[3]="http://www.sitepoint.com";

randomlinks[4]="http://www.cnn.com";

function randomlink() {

window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]

}

window.onload=randomlink();

</script>[/QUOTE]
Copy linkTweet thisAlerts:
@magentaplacentaDec 19.2007 — That works on page load, but you can just remove window.onload=randomlink(); and put that function call in a button:

<button onclick="randomlink()">random site</button>
×

Success!

Help @mona979 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...