Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Why is Switch Menu broken only with www's in the url?

  1. #1
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Why is Switch Menu broken only with www's in the url?

    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.

  2. #2
    Join Date
    Aug 2004
    Posts
    10,140
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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.

  3. #3
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    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?

    All help is GREATLY appreciated!

  4. #4
    Join Date
    Aug 2004
    Posts
    10,140
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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.

  5. #5
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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...

  6. #6
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    I think this can be worked around. I'm not great with cookies,but try this.
    Replace
    Code:
    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
    Code:
    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;
    }

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Aug 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink

    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!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    if(location.href.indexOf('www.')!==7)
    location.href=location.href.replace(/http:\/\//, 'http://www.');
    to switch to www from non www or:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Aug 2004
    Posts
    10,140
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    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.

Bookmarks

Posting Permissions

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