Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Why this seasonal JavaScript not working??

    Hi there,

    could anybody check why this script is not working please?
    I tried almost everythink...

    Code:
    <script>
    
    function load_script(url) {
       var e = document.createElement("script");
       e.src = url;
       e.type = "text/javascript";
       document.getElementsByTagName("head")[0].appendChild(e);
    }
    
    window.onload = function( ) {
    	var date = new Date;
    	var month = date.getMonth( ) + 1;
    
    	if ((6 <= month) || (3 >= month)) {
    		load_script("season.js");
    	}
    }
    
    </script>
    Many thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Code:
    document.getElementsByTagName("head")[0].appendChild(e.src);
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still not working but thank you for your hint.

  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    Your original script works just fine for me using chrome.

    I get this in the head
    Code:
    <script src="season.js" type="text/javascript"></script>
    Do you have the file path correct for your season.js?

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #5
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    path is correct but this script should load snow.js only between 6 - 3 month of the year.
    If i put
    Code:
    <script src="season.js" type="text/javascript"></script>
    it will load every time. This is not what I want. But thank you.

  • #6
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    So only load the script if month is 3 or more AND month is 6 or less? ie between 3 and 6 including 3 and 6.

    Code:
    <script>
    function load_script(url) {
       var e = document.createElement("script");
       e.src = url;
       e.type = "text/javascript";
       document.getElementsByTagName("head")[0].appendChild(e);
    }
    
    window.onload = function( ) {
    	var date = new Date;
    	var month = date.getMonth( ) + 1;
    
    	if ((month >= 3) && (month <= 6)) {
    		load_script("season.js");
    	}
    }
    </script>
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #7
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am trying to load that script only in winter months. I put ((month >= 3) && (month <= 6)) because I would like to test it if it works. But the script not working for me. I don't know why...

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Well, the current month is August 7 + 1 = 8.

    Your logic is incorrect. Should be

    Code:
    if ((month <=3) || (month >= 6)) {   // months 1,2,3 6,7,8,9,10,11,12
    You seem to have long winters where you come from!
    Last edited by Philip M; 08-19-2013 at 09:53 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    #Philip M
    Your logic is incorrect.
    Yeh, I misunderstood. Sorry.

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • #10
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply but why it isn't working? If I set
    Code:
    ((month <=3) || (month >= 6))
    and now is 19th of August it should run the script but it doesn't.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by ren7o View Post
    Thanks for the reply but why it isn't working? If I set
    Code:
    ((month <=3) || (month >= 6))
    and now is 19th of August it should run the script but it doesn't.
    Code:
    window.onload = function( ) {
    var date = new Date;
    var month = date.getMonth( ) + 1;
    alert (month); // 8 
    if ((month <=3) || (month >= 6)) {
    alert ("OK");
    }
    }
    Is the script in the correct directory?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi thank you Philip M. This script really works!
    I found there is maybe a problem with window.onload function in snow.js
    Could you look on it please? It is so puzzled.

    here it is:

    Code:
    http://tomak.borec.cz/snow.js

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,594
    Thanks
    78
    Thanked 4,386 Times in 4,351 Posts
    You can't use window.onload in that script.

    By the time the code is run to put that script in place, window.onload has already occurred.

    Your test for browserok is VERY VERY OLD and UNNEEDED code. Get rid of it. *ALL* modern browsers will support that script.

    But, in general, that script is too old to be worth using. It is *STILL* using document.write() which became essentially obsolete and unnecessary about 15 years ago!

    Personally, instead of conditionally including the <script>, I would just *ALWAYS* include it (it's a small script, after all) but then only *RUN* the script when the time of year is right.

    And there is so much *CRAP* unneeded code in there, in any case, it could be twice as small as it is.

    Throw it out. Start over.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,594
    Thanks
    78
    Thanked 4,386 Times in 4,351 Posts
    Other things wrong with that code: All the "snowflakes" fall at the same speed. Yuck. It uses a character ("." in your version, "*" in the recommended version) instead of *IMAGES* for the flakes! (If you use real images, they look better and you don't need as many.)

    Oh, w.t.h. Let me see if I can find my old snowfall code that looks and works much better.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,594
    Thanks
    78
    Thanked 4,386 Times in 4,351 Posts
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •