Changing Random quote display to non-random - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Changing Random quote display to non-random

suoidutssuoiduts Posts: 2Member
I have a site that currently uses JavaScript to randomly display a quote from an array of quotes each time the page is loaded. However, since this often results in the same quote being displayed several times in a row, I have decided that I would rather just cycle through the quotes in the array non-randomly each time the page is loaded. Unfortunately, I can't seem to find any sample code that would allow me to do this.

In a previous post on this board entitled "Simple Content Rotation" someone suggested the following code (in this case the poster just had two different content options):

[code]var l= true;
function changeContent(){
l=!l
if (l) {m="first content";}
else {m="second content";}
return(m)}
[/code]

I was going to use this as a starting point, but I must be using it wrong, because I always end up with "second content". Any suggestions, using this code or any other, would be appreciated.

Thank you.

Comments

  • zibadianzibadian Posts: 6,349Member
    : I have a site that currently uses JavaScript to randomly display a
    : quote from an array of quotes each time the page is loaded. However,
    : since this often results in the same quote being displayed several
    : times in a row, I have decided that I would rather just cycle
    : through the quotes in the array non-randomly each time the page is
    : loaded. Unfortunately, I can't seem to find any sample code that
    : would allow me to do this.
    :
    : In a previous post on this board entitled "Simple Content Rotation"
    : someone suggested the following code (in this case the poster just
    : had two different content options):
    :
    : [code]: var l= true;
    : function changeContent(){
    : l=!l
    : if (l) {m="first content";}
    : else {m="second content";}
    : return(m)}
    : [/code]:
    :
    : I was going to use this as a starting point, but I must be using it
    : wrong, because I always end up with "second content". Any
    : suggestions, using this code or any other, would be appreciated.
    :
    : Thank you.

    Your code itself is correct, but the problem lies with the way browsers handle javascript. Each time a page reloads all the javascript variables within that page are lost and all the javascript code runs again.
    Knowing this, you need to store the value in some more permanent location. There are four options for this:
    - the URL: www.mysite.com/mypage.html?quote=6
    - the server: Server side scripting necessary
    - a cookie: http://www.w3schools.com/js/js_cookies.asp
    - a hidden frame: Frame without any width or height
  • CyGuyCyGuy Posts: 312Member
    Yeah, hidden frames get your site black listed fast. Yet and Still, the concept of programming IS that you start back at one (or 0) for each call.

    As usual Zibidian is right. Javascript is not capable on its own of accomplishing such a task

    : : I have a site that currently uses JavaScript to randomly display a
    : : quote from an array of quotes each time the page is loaded. However,
    : : since this often results in the same quote being displayed several
    : : times in a row, I have decided that I would rather just cycle
    : : through the quotes in the array non-randomly each time the page is
    : : loaded. Unfortunately, I can't seem to find any sample code that
    : : would allow me to do this.
    : :
    : : In a previous post on this board entitled "Simple Content Rotation"
    : : someone suggested the following code (in this case the poster just
    : : had two different content options):
    : :
    : : [code]: : var l= true;
    : : function changeContent(){
    : : l=!l
    : : if (l) {m="first content";}
    : : else {m="second content";}
    : : return(m)}
    : : [/code]: :
    : :
    : : I was going to use this as a starting point, but I must be using it
    : : wrong, because I always end up with "second content". Any
    : : suggestions, using this code or any other, would be appreciated.
    : :
    : : Thank you.
    :
    : Your code itself is correct, but the problem lies with the way
    : browsers handle javascript. Each time a page reloads all the
    : javascript variables within that page are lost and all the
    : javascript code runs again.
    : Knowing this, you need to store the value in some more permanent
    : location. There are four options for this:
    : - the URL: www.mysite.com/mypage.html?quote=6
    : - the server: Server side scripting necessary
    : - a cookie: http://www.w3schools.com/js/js_cookies.asp
    : - a hidden frame: Frame without any width or height


    [green]green]
  • suoidutssuoiduts Posts: 2Member
    Thanks for the suggestions, zibadian. A cookie solved the problem!

    : : I have a site that currently uses JavaScript to randomly display a
    : : quote from an array of quotes each time the page is loaded. However,
    : : since this often results in the same quote being displayed several
    : : times in a row, I have decided that I would rather just cycle
    : : through the quotes in the array non-randomly each time the page is
    : : loaded. Unfortunately, I can't seem to find any sample code that
    : : would allow me to do this.
    : :
    : : In a previous post on this board entitled "Simple Content Rotation"
    : : someone suggested the following code (in this case the poster just
    : : had two different content options):
    : :
    : : [code]: : var l= true;
    : : function changeContent(){
    : : l=!l
    : : if (l) {m="first content";}
    : : else {m="second content";}
    : : return(m)}
    : : [/code]: :
    : :
    : : I was going to use this as a starting point, but I must be using it
    : : wrong, because I always end up with "second content". Any
    : : suggestions, using this code or any other, would be appreciated.
    : :
    : : Thank you.
    :
    : Your code itself is correct, but the problem lies with the way
    : browsers handle javascript. Each time a page reloads all the
    : javascript variables within that page are lost and all the
    : javascript code runs again.
    : Knowing this, you need to store the value in some more permanent
    : location. There are four options for this:
    : - the URL: www.mysite.com/mypage.html?quote=6
    : - the server: Server side scripting necessary
    : - a cookie: http://www.w3schools.com/js/js_cookies.asp
    : - a hidden frame: Frame without any width or height
Sign In or Register to comment.