six03 six03 - 1 year ago 112
Javascript Question

Random Quote Generator Dilemma

My code works fine on my pen at Codepen... Random Quote Generator at Codepen


I've been going over this at my site, but I'm just not seeing what the issue is. Random QuoteGenerator at my personal site

var quote = document.getElementById('quote');
var click = document.getElementById('click');
var quoteList = ["'You will never be happy if you continue to search for what happiness consists of. You will never live if you are looking for the meaning of life.'",
"'Do not read, as children do, to amuse yourself, or like the ambitious, for the purpose of instruction. No, read in order to live.'",
"'Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.'",
"'A life spent making mistakes is not only more honorable, but more useful than a life spent doing nothing.'",
"'In vain have I struggled. It will not do. My feelings will not be repressed. You must allow me to tell you how ardently I admire and love you.'",
"'It is better to remain silent at the risk of being thought a fool, than to talk and remove all doubt of it.'",
"'Have you ever been in love? Horrible isn't it? It makes you so vulnerable. It opens your chest and it opens up your heart and it means that someone can get inside you and mess you up.'",
"'The problem with the world is that the intelligent people are full of doubts, while the stupid ones are full of confidence.'",
"'Ok. You fuck me, then snub me. You love me, you hate me. You show me a sensitive side, then you turn into a total asshole. Is this a pretty accurate description of our relationship.'",
"'I love sleep. My life has the tendency to fall apart when I'm awake, you know?.'",
"'If I had a flower for every time I thought of you...I could walk through my garden forever.'",
"'Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great.'",
"'Never tell the truth to people who are not worthy of it.'",
"'The one you love and the one who loves you are never, ever the same person.'",
"'Becoming fearless isn't the point. That's impossible. It's learning how to control your fear, and how to be free from it.'",
"'I hope she'll be a fool -- that's the best thing a girl can be in this world, a beautiful little fool.'"];

var randomQuote = function(){
var twitter = document.getElementById('twitter');
var index = Math.floor(Math.random()*quoteList.length);
twitter.href = "" + encodeURI(quoteList[index]);
quote.innerHTML = quoteList[index];


click.addEventListener('click', randomQuote, false);

So the question is, what is going on? Same code, quotes aren't working on my personal site. Any help would be greatly appreciated. Thanks.

Answer Source

In your HTML script element, you're referencing the script with a href attribute when it should be src.

Also your code is executing before the DOM exists, move the Javascript to the end of the HTML body section.

    <script src="//" type="text/javascript"></script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download