Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2007
    Posts
    51
    Thanks
    1
    Thanked 1 Time in 1 Post

    Random Links target.

    I'm using a script that opens a random page once the link is clicked on.

    Code:
    <script type="text/javascript">
    function randomlinks(){
    var randomlink=Math.round(Math.random()*3)
    var link=new Array()
    link[1]="http://randomlink1"
    link[2]="http://randomlink2"
    link[3]="http://randomlink3"
    
    window.location=movie[randomlink]
    }
    </script>
    
    <a href="#" onClick="randomlinks()">Random Link</a>
    Now this works fine, but is there a way to get the link to open in a new tab.

    I try

    Code:
    <a href="#" onClick="randomlinks()" target="_blank">Random Link</a>
    But all that does is open '#' in a new tab. and the random links in the current tab.

    Thanks in advance.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Try this instead:

    Code:
    <script type="text/javascript">
    var link=new Array()
    link[0]="http://randomlink1"
    link[1]="http://randomlink2"
    link[2]="http://randomlink3"
    
    function randomLinks(elem){
      var randomLink=Math.floor(Math.random()*2);
      elem.href=movie[randomLink];
    }
    </script>
    
    <a href="#" onclick="randomLinks(this)">Random Link</a>
    Eric
    Last edited by A1ien51; 12-27-2007 at 03:15 PM. Reason: typo
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Dec 2007
    Posts
    51
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thanks for the help, but that code isn't working for me.

    I used this as a test.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body><script type="text/javascript">
    var link=new Array()
    link[0]="http://www.google.com"
    link[1]="http://www.yahoo.co.uk"
    link[2]="http://www.msn.com"
    
    function randomLinks(elem){
      var randomLink=Math.floor(Math.random()*2);
      elem.href=movie[randomLink];
    }
    </script>
    
    <a href="#" onclick="randomLinks(elem)">Random Link</a>
    </body>
    </html>
    Once i click on the link. Nothing happens

    --------------------

    With the random link that i am going to use, there will be over 500 links.

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    sorry about that, I messed up the onclick. should have been "this" and not "elem". I changed the code in my last post.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    New Coder
    Join Date
    Dec 2007
    Posts
    51
    Thanks
    1
    Thanked 1 Time in 1 Post
    It still won't work.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body>
    <script type="text/javascript">
    var link=new Array()
    link[0]="http://randomlink1"
    link[1]="http://randomlink2"
    link[2]="http://randomlink3"
    
    function randomLinks(elem){
      var randomLink=Math.floor(Math.random()*2);
      elem.href=movie[randomLink];
    }
    </script>
    
    <a href="#" onclick="randomLinks(this)">Random Link</a>
    
    </body>
    </html>
    I click on the link and nothing happens, other than the address bar changes to http://theurl.com#

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    The array is called link not movie. This works fine
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <script type="text/javascript">
    var movie=new Array()
    movie[0]="http://randomlink1"
    movie[1]="http://randomlink2"
    movie[2]="http://randomlink3"
    
    function randomLinks(elem)
    {
    	var randomLink=Math.floor(Math.random()*2);
    	elem.href=movie[randomLink];
    	elem.target = "_blank";
    }
    </script>
    <a href="#" onclick="randomLinks(this)">Random Link</a>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    Gilligan (01-04-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •