Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Display Random URL from list

  1. #1
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Display Random URL from list

    Hi,

    I'm looking for a script that would pick a URL from a predefined list and display the associated text of the link so that a viewer can click on it and be taken to the linked page.

    The format of the URL's I have is:
    <a href='http://mysite.com target='_blank'> A Day in Alexandria</a> alexandria, Egypt<BR>

    Now, I have about 600 such links and need a script that would pick one and display it to the viewer.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    (function(){
    	var myLinks = [
    	{href: 'http://www.whatever.com/somepage.htm', text: 'Some Page'},
    	{href: 'http://www.whatever.com/anotherpage.htm', text: 'Another Page'},
    	{href: 'http://www.whatever.com/yetanotherpage.htm', text: 'Yet Another Page'} // <--No comma after last link object
    	];
    	onload = function(){
    		var link = myLinks[Math.floor(Math.random() * myLinks.length)],
    		pageLink = document.getElementById('myLink');
    		pageLink.href = link.href;
    		pageLink.firstChild.nodeValue = link.text;
    		pageLink.target = '_blank';
    	};
    })();
    </script>
    </head>
    <body>
    <div>
    <a id="myLink" href="somedefault.htm">Some Default Text</a>
    </div>
    </body>
    </html>
    Any questions, just let me know.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    John, Thank you so much for this.

    I've added the script and div things into my page, but it just keeps showing "Some Default Text" and upon clicking gives a 404 not found. Pls do take a look at this link (http://tinyurl.com/ybmm389) and let me know where I'm going wrong.

    Regards.

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    There's an error on one of the lines. It's ended prematurely because of an apostrophe.

    Error: missing } after property list
    Source File: http://www.africadreamholidays.com/script_test.htm
    Line: 305, Column: 104
    Source Code:
    {href: 'http://reservations.bookhostels.com/tours/africadr/tourdetails.php?TourUID=7338', text: 'Fellah's Tent (Jolie Ville Luxor). - Luxor, Egypt'},

    Try:

    Code:
    {href: 'http://reservations.bookhostels.com/tours/africadr/tourdetails.php?TourUID=7338', text: 'Fellah\'s Tent (Jolie Ville Luxor). - Luxor, Egypt'},

  5. #5
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    There's an error on one of the lines. It's ended prematurely because of an apostrophe.
    WOW!! that would mean that wherever there is an apostrophe in the text part, it would give a problem.

    Thanks so much

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Schmoopy View Post
    There's an error on one of the lines. It's ended prematurely because of an apostrophe.

    Error: missing } after property list
    Source File: http://www.africadreamholidays.com/script_test.htm
    Line: 305, Column: 104
    Source Code:
    {href: 'http://reservations.bookhostels.com/tours/africadr/tourdetails.php?TourUID=7338', text: 'Fellah's Tent (Jolie Ville Luxor). - Luxor, Egypt'},

    Try:

    Code:
    {href: 'http://reservations.bookhostels.com/tours/africadr/tourdetails.php?TourUID=7338', text: 'Fellah\'s Tent (Jolie Ville Luxor). - Luxor, Egypt'},
    That's correct, but there are also a few more like that:

    Code:
    'Baz Bus Cape Town <=> Jo'burg VIA SWAZILAND. - Cape Town, South Africa'},
    'Baz Bus Cape Town <=>Jo'burg VIA DRAKENSERG. - Cape Town, South Africa'},
    'Baz Bus Cape Town <=> Jo'burg VIA SWAZILAND. - Durban, South Africa'},
    Which should be:

    Code:
    'Baz Bus Cape Town <=> Jo\'burg VIA SWAZILAND. - Cape Town, South Africa'},
    'Baz Bus Cape Town <=>Jo\'burg VIA DRAKENSERG. - Cape Town, South Africa'},
    'Baz Bus Cape Town <=> Jo\'burg VIA SWAZILAND. - Durban, South Africa'},
    Basically, whenever you use a like delimiter within a string, it must be escaped with the down slash.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    How did you find them so quickly? any trick?
    Thanks, man.

  8. #8
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Best thing to do with errors like these is, if you're using firefox, go to the error console. Or use firebug, shows you exactly where the script is going wrong.

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by fay123 View Post
    How did you find them so quickly? any trick?
    Thanks, man.
    I looked at the page's source code in an editor (Edit Pad Pro, but there are many others) that highlights script code. Strings are blue, most other code is black, delimiters and operators are red. Then by simply scrolling through your list, the broken strings just popped out, they're party black where they shouldn't be.

    An alternative for dealing with apostrophe is, for example:

    Code:
    "Baz Bus Cape Town <=> Jo'burg VIA SWAZILAND. - Cape Town, South Africa"},
    In fact all the href's and text's could be quoted with double quotes, or you can mix and match, just be sure to be consistent in any one value, like don't do:

    Code:
    text: "This would be a mistake'},
    But this would be fine:

    Code:
    {href: 'somepage.htm', text: "this'll work OK"},
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Nov 2009
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    the script is still giving problem, so I checked it with firefox's error console, and this is what it says:
    Code:
    Error: missing } after property list
    Source File: http://www.africadreamholidays.com/
    Line: 516, Column: 121
    Source Code:
    {href: 'http://reservations.bookhostels.com/tours/africadr/tourdetails.php?TourUID=732', text: 'Baz Bus Cape Town <=> Jo'burg VIA SWAZILAND. - Cape Town, South Africa'},
    so I checked the htm file and this particular line seems fine
    Here's a straight copy and paste of the line from the 'view source code':
    Code:
    {href: 'http://reservations.bookhostels.com/tours/africadr/tourdetails.php?TourUID=732', text: 'Baz Bus Cape Town <=> Jo\'burg VIA SWAZILAND. - Cape Town, South Africa'},

Bookmarks

Posting Permissions

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