0

Hi I have been trying to figure out how to use a random quote script, where each quote is its on hyperlink going to a different page. So far this is the example I'm using for the quotes. is there a way to make each quote its own hyper link?

<script language="JavaScript">

var Quotation=new Array() // do not change this!


Quotation[0] = "Test.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";


var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>
Paul
  • 139,544
  • 27
  • 275
  • 264
  • how do you determine which page to go? – Eric Fortis Jun 12 '12 at 00:38
  • 1
    Yes there is. Either create HTML or DOM elements with JavaScript. It's one of the typical uses of JS and you should be able to find lots of examples on the net. For example: http://stackoverflow.com/questions/8005694/make-hyperlink-from-javascript – Felix Kling Jun 12 '12 at 00:40
  • i have no clue where this example comes from but it was given to me at school, from a classmate. – user1450117 Jun 12 '12 at 00:40
  • i tried looking for examples but all the links i keep getting are for one or the other and not togeather. – user1450117 Jun 12 '12 at 00:42

1 Answers1

0

This script will replace itself with an <a> tag with a random quote and corresponding href:

<script>
(function(){

    var quotes = [
      {text: "Test.", href: "http://example.com"},
      {text: "Sanity is a golden apple with no shoelaces.", href: "http://example.com"},
      {text: "Repent! The end is coming, $9.95 at Amazon.", href: "http://example.com"},
      {text: "Honesty blurts where deception sneezes.", href: "http://example.com"},
      {text: "Pastry satisfies where art is unavailable.", href: "http://example.com"},
      {text: "Delete not, lest you, too, be deleted.", href: "http://example.com"},
      {text: "O! Youth! What a pain in the backside.", href: "http://example.com"},
      {text: "Wishes are like goldfish with propellors.", href: "http://example.com"},
      {text: "Love the river's \"beauty\", but live on a hill.", href: "http://example.com"},
      {text: "Invention is the mother of too many useless toys.", href: "http://example.com"}
    ];

    var scripts = document.getElementsByTagName('script');
    var script = scripts[scripts.length - 1];
    var quote = quotes[Math.floor(Math.random()*quotes.length)];
    var a = document.createElement('a');
    a.href = quote.href;
    a.appendChild(document.createTextNode(quote.text));
    script.parentNode.replaceChild(a, script);

})();
</script>

JSFiddle

This is the best way to do it if you just want to put the script where you want the link to be and you only want this once on a page. If you want it more than once on a page you can do this a bit differently so that you don't need multiple copies of the script.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • ok, useing the code i posted above, where would i implement this with the script above? – user1450117 Jun 12 '12 at 02:21
  • @user1450117 Where would you implement what? Just copy and paste this script where you want your link to be, and change all the `http://example.com`s to the correct urls. You can also check the JSFiddle. – Paul Jun 12 '12 at 02:25
  • i did copy and paste and changed everything but when i go to test it, it shows up as a blank page. – user1450117 Jun 12 '12 at 02:31
  • @user1450117 What part of the page did you put it in? It should be somewhere in the body. – Paul Jun 12 '12 at 02:37
  • i tried the jsfiddle page, and it seems to work, now when i save it and to try and open it, it shows up blank, is there something im missing to do? – user1450117 Jun 12 '12 at 02:50
  • @user1450117 What do you mean by save it? You should just take an existing HTML page (which isn't blank) and then paste this script somewhere in the HTML (wherever you want the `` to go). – Paul Jun 12 '12 at 03:15