Results 1 to 2 of 2

Thread: Stylesheet Switcher

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

    Question Stylesheet Switcher

    IM USING DD STYLE SWITCHER BUT IT WONT TRACK OVER SUB.DOAMINS, I THINK I HAVE SOMETHING WRONG IN COOKIE BUT NOT SURE WHERE.


    Code:
    //Style Sheet Switcher version 1.0 Nov 9th, 2005
    //Author: Dynamic Drive: http://www.dynamicdrive.com
    //Usage terms: http://www.dynamicdrive.com/notice.htm
    
    function getCookie(Name) { 
    var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
    return document.cookie.match(re)[0].split("=")[1] //return its value
    return null
    }
    
    function setCookie(name, value, days) {
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
    document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=\";
    }
    
    function deleteCookie(name){
    setCookie(name, "moot")
    }
    
    function setStylesheet(title) {
    var i, cacheobj
    for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
    if(cacheobj.getAttribute("rel").indexOf("style") != -1 && cacheobj.getAttribute("title")) {
    cacheobj.disabled = true
    if(cacheobj.getAttribute("title") == title)
    cacheobj.disabled = false //enable chosen style sheet
    }
    }
    }
    
    function chooseStyle(styletitle, days){
    if (document.getElementById){
    setStylesheet(styletitle)
    setCookie("mysheet", styletitle, days)
    }
    }
    
    function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu 
    var i
    if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu
    var element=(element.type=="select-one") ? element.options : element
    for (i=0; i<element.length; i++){
    if (element[i].value==selectedtitle){ //if match found between form element value and cookie value
    if (element[i].tagName=="OPTION") //if this is a select menu
    element[i].selected=true
    else //else if it's a radio button
    element[i].checked=true
    break
    }
    }
    }
    }
    
    var selectedtitle=getCookie("mysheet")
    if (document.getElementById && selectedtitle!=null) //load user chosen style sheet if there is one stored
    setStylesheet(selectedtitle)

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

    Javascript cookies can only be used on the domain that they were set on. And then, only if they were set for the domain (as opposed to for the page). Otherwise, they are only available for the page that set them.

    Your code looks like it sets the cookie for the domain so, the problem is probably that whatever you are calling a sub-domain is viewed in javascript as a separate domain.

    What exactly do you mean by a sub-domain? If it has a different DNS, then it is a separate domain.

    I'm not sure about the following but, it may be true that if you have a page here:

    /pages/stuff/page.htm

    that setting a cookie there may not be able to be read on a page here:

    /pages/otherstuff/page.htm

    Is that what is happening?
    - John
    ________________________

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

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
  •