PDA

View Full Version : Why is Switch Menu broken only with www's in the url?



aplayerb
09-07-2006, 10:49 PM
1) Script Title: Switch Menu

2) Script URL (on DD): http://dynamicdrive.com/dynamicindex1/switchmenu.htm

3) Describe problem:

I have noticed that the menu works great with no www's in the url. As you click all links under different buttons, it remembers which list of links to keep expanded.

Click for working example: http://abinterfaces.com/joyceconnell/index.htm

However when you DO HAVE www's in the url, and click all links under different buttons... the menu will always expands the same list of links only.

Click here for broken example: http://www.abinterfaces.com/joyceconnell/index.htm

Can someone PLEASE help me? I am almost finished building 2 websites using this menu which now appears broken. If I can't figure it out I'm in trouble. :(

ddadmin
09-08-2006, 01:07 AM
I think what you mean is that two different persistence settings seem to be in place, one for the page without the "www", and the other, with? This is a cookies issue with the browser- the same page accessed these two ways are treated as different pages, so two cookies get set.

aplayerb
09-08-2006, 01:31 AM
That does seem logical, however... even if I use an application to clear cache & temp files before trying it either way, the problem still persists. It happens no matter what I do or how much I clear my cookies. Try it for yourself and you'll see what I mean. Any thoughts? :o

All help is GREATLY appreciated!

ddadmin
09-08-2006, 01:33 AM
Clearing your cookies won't help, since this is a browser behavior. It basically treats:

http://www.yahoo.com and
http://yahoo.com

as two seperate pages. Due to this, a different cookie will be used in each case, resulting in different persistent settings. At least from what I understand of cookies, this is the situation.

aplayerb
09-08-2006, 01:37 AM
Does that mean I have to accept the fact that when a user types the www's the menu will not work correctly? I'm a bit confused...

blm126
09-08-2006, 02:09 AM
I think this can be worked around. I'm not great with cookies,but try this.
Replace


function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
document.cookie=cookiename+"="+cookievalue
}

from step one. With this


function savemenustate(){
var inc=1, blockid=""
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc
break
}
inc++
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
var loc = RemoveSub(window.location.host);
cookievalue += ";domain="+loc
document.cookie=cookiename+"="+cookievalue
}
function RemoveSub(url){
var ua = url.split('.');
if(ua.length > 2){
ua.shift();
}
var uaj = ua.join('.');
return uaj;
}

jscheuer1
09-08-2006, 07:19 AM
I think aplayerb doesn't fully understand how this script's persistence feature works. In Opera, not at all. In IE, it doesn't matter if it is www or not. In FF it matters if www is used or not but, this is not so bad as it seems because if a user accesses the site using www to begin with, unless you are using absolute links on your pages to switch it to a non www URL, they will continue for the session as www. In that case the persistence will work as long as they keep with www. If a user starts out without www, persistence will also work. It is only the alternating between the two that causes confusion in FF. So, use relative URLs on your pages and the users will have to go out of their way to switch.

Add to this the fact that most users will not switch between www and not www on their own and that IE is the most widely used browser with FF the dominant second most used and you will see that this is hardly much of a concern unless you have some URLs hard coded onto your site with http://www/domain . . . and some hard coded http://domain . . . - not a good idea to begin with.

aplayerb
09-08-2006, 12:46 PM
Thank you all for your help. I will try the modified code. I also agree that 99.9 will only come in with or without the www's and never experience the issue. I just hate putting my name on anything that could ever appear glitchy to my client.

Thanks again!

jscheuer1
09-08-2006, 05:25 PM
I'm not real confident that the modification offered by blm126 would work, though it may. I haven't tested it and haven't even read it over enough to form a direct opinion. My reservations come from the fact that I don't think a browser will access a cookie that it thinks is from another domain while not on that domain - and that cookies are odd beasts, not always as easily manipulated as might at first appear.

Either way, another solution, if you have a server side language available would be to have all your pages set to switch to www or to not www. This could also be done with javascript, if the only reason for doing so is to help another javascript to work. Something like:


if(location.href.indexOf('www.')!==7)
location.href=location.href.replace(/http:\/\//, 'http://www.');

to switch to www from non www or:


if(location.href.indexOf('www.')==7)
location.href=location.href.replace(/www\./, '');

to switch to non www from www.

Also, since you seem concerned about 100% interoperability, the cookie can be made to work in Opera (and probably others that may be quirky at times with onunload) if it is dropped each time the menu state changes instead of only 'onunload' which doesn't always fire as expected in Opera.

ddadmin
09-08-2006, 07:39 PM
I haven't tested out blm126 neither, but just to explain the issue at hand again, in some browsers, "www" and no "www" for an identical URL can trigger the browser into thinking they are two different pages, hence when a script tries to store some value associated with the page into a cookie, two cookies are used by the browser, instead of the desired one. This doesn't mean the script itself is broken, or even the browser- it's just a difference in behavior from browser to browser as far as cookies are concerned.

For example, when logging out of DD forums, you may need to do it twice, by accessing both www.dynamicdrive.com/forums and dynamicdrive.com/forums. The problem is the same.

blm126
09-08-2006, 07:49 PM
Here (http://www.quirksmode.org/index.html?/js/cookies.html) is an interesting resource on cookies. It discusses this problem under the section "Domain and path"