1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.5 Make javascript only work on specific dates possible?

Discussion in 'XenForo Questions and Support' started by borgqueenx, Dec 7, 2015.

  1. borgqueenx

    borgqueenx Active Member

    Im having some javascripts on my site (snow effect) and it works/gets called by an edit in a template file.
    I want it to work on between 20 december and 20 january. is there a way to do this?
     
  2. Brogan

    Brogan XenForo Moderator Staff Member

    Yes there's a way but why not just edit the template on 20 December and revert it on 20 January?
     
    Amaury likes this.
  3. borgqueenx

    borgqueenx Active Member

    I want to automate as much as i can:)
     
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    Code:
    if (Date.now() >= Date(2015, 11, 20) && Date.now() <= Date(2016, 0, 20)) {
      // your javascript here
    }
     
  5. borgqueenx

    borgqueenx Active Member

    Is it possible to remove the year and will it then still work?
     
  6. Jeremy

    Jeremy XenForo Moderator Staff Member

    No.

    But you could generalize it by replacing it with Date.prototype.getFullYear()

    Code:
    var now = Date.now();
    if (now >= Date.parse(Date(now.getFullYear(), 11, 20)) && now <= Date.parse(Date(now.getFullYear() + 1, 0, 20))) {
    // code
    }
     
    Last edited: Dec 8, 2015
  7. borgqueenx

    borgqueenx Active Member

    That will make it work for every year? Thanks!
     
  8. Jeremy

    Jeremy XenForo Moderator Staff Member

    I haven't tested it, but yes, in theory.
     
  9. borgqueenx

    borgqueenx Active Member

    it doesnt work. i quickly tried to make 20 december into 7 december and the effect is not starting :/
     
  10. Jeremy

    Jeremy XenForo Moderator Staff Member

    I've updated the code, it appears that using Date as a constructor gives a string and not a milliseconds/object. Just checked it out in the console and it works.
     
  11. Jake B.

    Jake B. Well-Known Member

    Really unrelated to this thread, but if you're going to do that snowfalling thing, it may be a good idea to create a custom user field that will let users disable that as it can get fairly annoying if you're trying to read something :)
     
  12. borgqueenx

    borgqueenx Active Member

    this is what i have and not working currently.
     
  13. borgqueenx

    borgqueenx Active Member

    will be difficult....instead im just having the effect small/slightly so its not that intrusive.
     
  14. Jake B.

    Jake B. Well-Known Member

    Not difficult at all, create a custom user field with the id "disableSnowfall" and do this:

    Code:
    <xen:if is="!{$visitor.customFields.disableSnowfall}">
    [Your JS here]
    </xen:if>
    
     
  15. borgqueenx

    borgqueenx Active Member

    still doesn't tell me everything. what javascript to put there? if i understand correctly this makes it always work or always disabled. I want it only work during my specific time, and then that the user has the option to disable it.
     
  16. Jeremy

    Jeremy XenForo Moderator Staff Member

    Reasons I hate JavaScript...

    Code:
    var now = new Date();
    if (now >= new Date(now.getFullYear(), 11, 20) && now <= new Date(now.getFullYear() + 1, 0, 20)) {
    // code
    }
    
    Screen Shot 2015-12-08 at 5.11.24 PM.png
     
  17. James

    James Well-Known Member

    Jake's point is that you should give the users the ability to disable it. Put the javascript between the conditional statement he provided and people can disable it if they don't want it.
     
  18. borgqueenx

    borgqueenx Active Member

    currently the script is in a template logo_block. so adding it to the user's field will probaly not work. i think.
     
  19. borgqueenx

    borgqueenx Active Member

    tried this again, to no success... :p
     
  20. Jeremy

    Jeremy XenForo Moderator Staff Member

    I attached a picture of the code being run and functioning. What browser are you on?
     

Share This Page