Weird Javascript behavior

Discussion in 'JavaScript' started by Funnycoolstuff, Sep 30, 2007.

  1. #1
    I have a Random Quote feature which works through a Javascript file. You can see it at the right column on this page http://www.wise-quotes.com/

    However, I don't know why the Javascript file doesn't always run, you can see no random quote being displayed when it doesn't run. You need to refresh a few times, then you can see the random quote.

    However, if you browse the Javascript file directly (http://www.wise-quotes.com/tools/random-quote.js), you can see random quote being generated every time!

    Why? Any idea?

    Thanks.
     
    Funnycoolstuff, Sep 30, 2007 IP
  2. code-rush

    code-rush Guest

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    That's maybe because your script is located in the body tag?
    Try to add it in the head tag, generate the quote, create an element (a span or a p) and append the element created to the quote container.

    something like this:

    in the script file:

    // create the element
    var theSpan = document.createElement("span");
    
    // add the quote to span
    // you could use a function to generate the quote text
    var theContent = "your quote here";
    
    theSpan.appendChild(theContent);
    
    
    // When the page loads, append the Span to the quote container 
    
    var container = document.getElementById("you quote container id");
    container.appendChild(theSpan);
    Code (markup):
     
    code-rush, Oct 1, 2007 IP
  3. Funnycoolstuff

    Funnycoolstuff Guest

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    I am not too sure if I know how to do this. But basically, I need to add something like:

    <span name="you quote container id"></span>
    Code (markup):
    at the place where I want to show the quote?

    Anyway, this may not be the cause of the problem because I got the idea to do this from another quote website. The js file is just like what I have, and it works good. I mean, that js file can be placed wherever I want in the body, no need anything in the head tag.
     
    Funnycoolstuff, Oct 1, 2007 IP