Web Analytics Made Easy -
StatCounter Javascript Code Rotator? - CodingForum

Announcement

Collapse
No announcement yet.

Javascript Code Rotator?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript Code Rotator?

    Hi Coders,

    I'm in desperate need of some JS code that will rotate "two" snippets of javascript code 50/50. So basically every time someone visits my site i need it to load either snippet 1 or snippet 2. I tried doing this before with an array but the javascript caused the page to not load because i think both snippets of code where trying to be executed at the same time? Can anyone code me up something that will rotate two snippets of js code without having them both try to load at the same time on the browser.

    Need your help!!!
    Thanks.

  • #2
    Surely someone must know how to do this?

    Comment


    • #3
      Surely, you must learn that patience is a virtue. Forty-five minutes wait before bumping your thread doesn't show that you're willing to give people a chance to answer, perhaps even test something before giving you a solution. It may seem simple to you, but it may not be as simple as you think it is. Anyways, here is _something_ that might or might not work... (untested):

      Code:
      var randomnumber=Math.floor(Math.random()*1)
      if (randomnumber == 0) {
      // code for snippet 1
      }
      else {
      // code for snippet 2
      }
      Try it, see how that goes for you... and once again, try and learn to be a bit more patient.
      The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

      How to ask smart questions?

      Comment


      • #4
        Code:
        document.write("<script src='" +['homework.js','cheat.js'][new Date().getTime()%2]+"' type='text/javascript'><\/script>");

        Comment


        • #5
          Originally posted by Eldarrion View Post
          Surely, you must learn that patience is a virtue. Forty-five minutes wait before bumping your thread doesn't show that you're willing to give people a chance to answer, perhaps even test something before giving you a solution. It may seem simple to you, but it may not be as simple as you think it is. Anyways, here is _something_ that might or might not work... (untested):

          Code:
          var randomnumber=Math.floor(Math.random()*1)
          if (randomnumber == 0) {
          // code for snippet 1
          }
          else {
          // code for snippet 2
          }
          Try it, see how that goes for you... and once again, try and learn to be a bit more patient.
          That code don't work, i tried it already b4. Thanks for your effort!, i will try the other code poster here.

          Comment


          • #6
            Originally posted by randomuser773 View Post
            Code:
            document.write("<script src='" +['homework.js','cheat.js'][new Date().getTime()%2]+"' type='text/javascript'><\/script>");
            I tried this code but, the external js file don't seem to load? It causes my page to show up blank?
            Last edited by blackxxxer; Apr 1, 2009, 06:19 PM.

            Comment


            • #7
              Originally posted by Eldarrion View Post
              Surely, you must learn that patience is a virtue. Forty-five minutes wait before bumping your thread doesn't show that you're willing to give people a chance to answer, perhaps even test something before giving you a solution. It may seem simple to you, but it may not be as simple as you think it is. Anyways, here is _something_ that might or might not work... (untested):

              Code:
              var randomnumber=Math.floor(Math.random()*1)
              if (randomnumber == 0) {
              // code for snippet 1
              }
              else {
              // code for snippet 2
              }
              Try it, see how that goes for you... and once again, try and learn to be a bit more patient.
              This code loads the javascript but only rotates 1, the 2nd one does not ever show up?

              Comment


              • #8
                Originally posted by blackxxxer View Post
                I tried this code but, the external js file don't seem to load? It causes my page to show up blank?
                It's intended to be run in-line. You must be calling it after the page has loaded.

                Comment


                • #9
                  Originally posted by randomuser773 View Post
                  It's intended to be run in-line. You must be calling it after the page has loaded.
                  How do i go about calling it once the page has loaded? Sorry for all the n00b questions.

                  Comment


                  • #10
                    Once again this idiotic forum has eaten one of my posts!

                    *SIGH*

                    Eldarrion's code is wrong!!!
                    Code:
                    var randomnumber=Math.floor(Math.random()*1)
                    if (randomnumber == 0) ...
                    the value of randomnumber will *ALWAYS* be ZERO!!!!

                    You *could* change it to
                    Code:
                    var randomnumber=Math.floor(Math.random()*2)
                    if (randomnumber == 0) ...
                    but the simpler way is to just do:
                    Code:
                    if ( Math.random() < 0.5 ) 
                    {
                       ... snippet 1 ...
                    } else {
                       ... snippet 2 ...
                    }
                    Be yourself. No one else is as qualified.

                    Comment


                    • #11
                      randomuser's code is *ONLY* for use as the page is being created. You can't use document.write once the page is loaded. (Well, you can, but it will wipe out ALL other page contents!)

                      His code is also only for choosing one of two different external ".js" files.

                      Which is fine, if you have your code organized that way.

                      But if your "snippets" are just JavaScript functions, and you want to call one or the other, then Eldarrion's code is the way to go...excepting for the major goof that I noted in last message.
                      Be yourself. No one else is as qualified.

                      Comment


                      • #12
                        Originally posted by blackxxxer View Post
                        How do i go about calling it once the page has loaded? Sorry for all the n00b questions.
                        Code:
                        function rsc()
                        {
                         (document.body.appendChild(document.createElement('script'))).src = ['homework.js','cheat.js'][new Date().getTime()%2];
                        }
                        
                        rsc();

                        Comment


                        • #13
                          Originally posted by randomuser773 View Post
                          Code:
                          function rsc()
                          {
                           (document.body.appendChild(document.createElement('script'))).src = ['homework.js','cheat.js'][new Date().getTime()%2];
                          }
                          
                          rsc();

                          Hi, thanks for the code, i'm a total noobie could you tell me where to place that function rsc code? do i need to place java script tags between that to get it to work? I need to stick the rotating code before the end of my </head> tag.
                          Last edited by blackxxxer; Apr 1, 2009, 07:46 PM.

                          Comment


                          • #14
                            Originally posted by blackxxxer View Post
                            Hi, thanks for the code, i'm a total noobie could you tell me where to place that function rsc code? do i need to place javascript tags between that to get it to work?
                            Of course you do - but the effect will be the same as the first code I gave you - it will run in-line as soon as it is rendered. You said you wanted to call it after the page loads, so what event or user-input will trigger it?

                            Comment


                            • #15
                              Originally posted by randomuser773 View Post
                              Of course you do - but the effect will be the same as the first code I gave you - it will run in-line as soon as it is rendered. You said you wanted to call it after the page loads, so what event or user-input will trigger it?
                              To be more clear.

                              I have two javascript snippets that look like this:

                              1. <script src="http://www.mydomain.com/mygateway.php?pub=00000&gateid=MjAwMw%3D%3D" language="JavaScript" type="text/javascript"></script>

                              2. <script src="http://www.mydomain.com/mygateway.php?pub=00000&gateid=MjAwMw%3D%3D" language="JavaScript" type="text/javascript"></script>


                              The snippets of code above must be placed between the <head></head> of my page. I need the code to rotate the two snippets to display 1 or the other. I need the code run after the pages loads i guess, because if i try to before the page will blank out on me for some reason. The event that the user needs to trigger is simply going to my page?

                              Comment

                              Working...
                              X