Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-14-2013, 04:42 PM   PM User | #1
Saymos
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Saymos is an unknown quantity at this point
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.
Saymos is offline   Reply With Quote
Old 01-14-2013, 06:20 PM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 710
Thanks: 31
Thanked 128 Times in 119 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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.
mrhoo is offline   Reply With Quote
Old 01-14-2013, 09:40 PM   PM User | #3
Saymos
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Saymos is an unknown quantity at this point
Made a double post, see below instead.

Last edited by Saymos; 01-14-2013 at 10:42 PM..
Saymos is offline   Reply With Quote
Old 01-14-2013, 09:49 PM   PM User | #4
Saymos
New to the CF scene

 
Join Date: Jan 2013
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Saymos is an unknown quantity at this point
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 http://www.codingforums.com/showthread.php?t=285721
Saymos is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:51 PM.


Advertisement
Log in to turn off these ads.