1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

refresh div in a web page - not whole page

Discussion in 'JavaScript' started by Peanutbutter45, Mar 17, 2015.

  1. #1
    I have a random phrase (js ideas) on my html page that works when I refresh the page.
    But I do not want to refresh the whole web page since other things are happening.
    It gets the phrases from an external js file.
    I need to refresh it every few seconds - 3 to 5 will work.
    How do I make this phrase dynamically refresh without refreshing the whole web page?

    <div align="center">
    <p class="p">
    <strong>What if
       <script language="JavaScript" type="text/javascript">
       //<![CDATA[
       document.write(ideas[rand(ideas.length)]);
       //]]>
       </script>
       <br />
        That might work.</strong>
    </p>
    </div>
    HTML:
    Any assistance would be appreciated.
    Thanks,
     
    Peanutbutter45, Mar 17, 2015 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    I'll assume that your "ideas" variable is already there and a JavaScript array...

    You could do something like this:
    var ideas = ["Idea 1", "Idea 2", "Idea 3", "Idea 4"];
    <div id="ideas"></div>
    
    function new_item()
    {
        document.getElementById("ideas").innerHTML = ideas[Math.floor(Math.random() * ideas.length)];
    }
    new_item();
    setInterval(new_item, 5000);
    HTML:
    That will insert a random item from the "ideas" array inside the div with id="ideas" property, and then change it every 5 seconds (5,000 milliseconds).
     
    digitalpoint, Mar 17, 2015 IP
  3. Peanutbutter45

    Peanutbutter45 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks,
    My array is in a long list.
    It starts with:

    // Display Random Quotes
    // Quotes Array Function
    function makeArray(len) {
    for (var k = 0; k < len; k++) this[k] = null;
    this.length = len;
    }
    // Quotation array
    ideas = new makeArray(301);

    ideas[0] = "first idea line is here.";
    and goes to
    ideas[300] = "last idea line is here.";

    It ends with:

    // Seed Function
    function rand(n) {
    seed = (0x015a4e35 * seed) % 0xfffffff;
    return (seed >> 20) % n;
    }

    // Random function
    var rightnow = new Date();
    var seed = rightnow.getTime() % 0x7fffffff;


    I don't think I can list them like your script line shows ("Idea 1", "Idea 2", "Idea 3", "Idea 4", etc). It would be too long since I have 301 of them.
    Or will your script get one line at a time from my list without all that?

    This is all very new to me.
    I was happy just figuring out how to do the random thing form a list.
    Is there a way to wrap my DIV in a timer?
     
    Last edited: Mar 17, 2015
    Peanutbutter45, Mar 17, 2015 IP
  4. Peanutbutter45

    Peanutbutter45 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
     
    Peanutbutter45, Mar 17, 2015 IP
  5. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #5
    You can generate the array however you want... Really no right or wrong way to do it. It basically just takes 1 random item from the array named "ideas" every 5 seconds. As far as how you *get* that array, that's up to you since that's going to just be all your random things to show.

    Not sure what you mean by a random timer? Like a countdown timer, or?
     
    digitalpoint, Mar 17, 2015 IP
  6. Peanutbutter45

    Peanutbutter45 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    My bad. I deleted that 'random' word after you answered.
    I was just looking for another way. I am truly new to this.
    So, how do I make my long list array work with your code?
     
    Peanutbutter45, Mar 17, 2015 IP
  7. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #7
    Just take out the line I had that defines the array. Since yours is named the same thing, it should be fine.
     
    digitalpoint, Mar 17, 2015 IP