FORUMS: list search recent posts

Javascript open new random tab

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nick Sommers
Javascript open new random tab
on Mar 2, 2013 at 12:00:38 am

Hi Creative Cows!

I am struggling with some Javascript. I have some great code that creates a hyperlink that will randomly link you to any one of 17 pages that I have predetermined.

Where I am struggling is that I want the random page to open in a new tab, in exactly the same way that it would if you were using the target=_blank command in HTML.


HERE IS THE HEAD CODE:


var links = new Array(17)
links[0] = "radio/r01.htm"
links[1] = "radio/r02.htm"
links[2] = "radio/r03.htm"
links[3] = "radio/r04.htm"
links[4] = "radio/r05.htm"
links[5] = "radio/r06.htm"
links[6] = "radio/r07.htm"
links[7] = "radio/r08.htm"
links[8] = "radio/r09.htm"
links[9] = "radio/r10.htm"
links[10] = "radio/r11.htm"
links[11] = "radio/r12.htm"
links[12] = "radio/r13.htm"
links[13] = "radio/r14.htm"
links[14] = "radio/r15.htm"
links[15] = "radio/r16.htm"
links[16] = "radio/r17.htm"
function go() {
var a = 1+Math.round(Math.random()*17)
var i = a
location = links[i]
}


HERE IS THE BODY CODE:
(I had to replace pointy brackets with square ones)

[a href="javascript:go()"]Radio Interview[/a]

Any javamasters out there?? :)) Cheers!!


Return to posts index

Curtis Thompson
Re: Javascript open new random tab
on Mar 2, 2013 at 5:36:21 pm

hello...

here's your page with that idea in action:

http://sitruc.com/cow/js_new_tab.html

note the comment in the js code (which you can delete, of course) that you can't guarantee a new tab vs a new window, but most browsers should play nice with that...

hope that helps! i also reduced your array code some as well and removed some variable setting redundancy...

sitruc


Return to posts index

Nick Sommers
Re: Javascript open new random tab
on Mar 3, 2013 at 4:48:30 pm

Wow Curtis that is some very neat code indeed!
Amazing thanks so much :)

Here is the site you have been helping me build:
http://www.meandmyguitar.com/blue/splash.htm

Send your address to me at shop@meandmyguitar.com me for a free CD
Cheers to you, Curtis!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]