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.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Merging two functions, how to get it to work?

    I'm using a script that with this code:

    Code:
    window.onunload = function(e) {
      var title = getActiveStyleSheet();
      createCookie("style", title, 365);
    }
    But then learned that window.onunload doesn't work for Chrome and Opera. Now I figured I could bake this part of the code into another function I have that looks like this:

    Code:
    function setActiveStyleSheet(title) {
      var i, a, main;
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
          a.disabled = true;
          if(a.getAttribute("title") == title) a.disabled = false;
        }
      }
    }

    But I'm quite new to javascripts and I'm not really sure on how to make it work and I would like some help on how the two functions could be done together.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    It's not that onunload doesn't work in those browsers, its that they both can load a page from the cache without firing the unload event on the window in view. You can bypass this, forcing an unload, by going outside the cache for a resource, or you can write the cookie before you return from the function. Move the line
    Code:
     createCookie("style", title, 365);
    to the other function.

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Made a double post, see below instead.
    Last edited by Saymos; 01-14-2013 at 10:42 PM.

  • #4
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the answer... The function works and creates a cookie like it is suppose to when I use this code:

    Code:
    function setActiveStyleSheet(title) {
      var i, a, main;
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
          a.disabled = true;
          if(a.getAttribute("title") == title) a.disabled = false;
        }
    	createCookie("style", title, 365);
      } 
    }

    But I still have the problem that my cookies doesn't load when I reload or reopen my page in Chrome. It works great in Firefox and IE but not Chrome. Do you know how to solve this? The full code used is presented in this question Code not reading cookies in Chrome


  •  

    Posting Permissions

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