PDA

View Full Version : Switch Content State memory with php templates



gekido
07-01-2005, 10:34 PM
Hello everyone,

Been using dynamic drive scripts for years, love them - best resource on the net...

Anyways, I've recently decided to use the 'switch content' state script for a new site in development.

I'm basically using the switch content script as a sidebar outlook style menu across the entire site...works great on all browsers etc...

However what I would like to do is get the script to remember whether the user has visited the site previously...

(script url: http://www.dynamicdrive.com/dynamicindex17/switchcontent.htm)

Yes the script already does this via a cookie, however my site management system is php driven, and as a result the path's for each page end up being unique, in the form http://www.mysite.com/?page=mypage.php

What I am looking to do is have the state remembered across ALL of the sub-pages of the domain, however the script, by default, only remembers the state for the current page that you are visiting.

so if the user visits mysite.com/?page=page1.php and opens/expands any of the side menu options - the second they click on a second link in the menu, it doesn't remember the state any more and contracts the entire side menu...

so the user can open/close any of the submenu options when they are visiting mysite.com/?page=page1.php, but then when they click a link (potentially to mysite.com/?page=page2.php) the menu closes and they end up having to reopen the menu options etc...

I've tried tweaking the javascript for the cookie handler so that it doesn't save or use the pathname when saving the cookie, but this hasn't fixed it...

eg:



function getselectedItem(){
//if (get_cookie(window.location.pathname) != ""){
//selectedItem=get_cookie(window.location.pathname)
if (get_cookie(window.location) != ""){
selectedItem=get_cookie(window.location)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}

//document.cookie=window.location.pathname+"="+selectedItem
document.cookie=window.location+"="+selectedItem
}

function do_onload(){
//uniqueidn=window.location.pathname+"firsttimeload"
uniqueidn=window.location+"firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}


note that this isn't the entire javascript, but you can see where i've commented the changed lines to remove the .pathname entries...

anyone know how i would get this script to save the expanded/contracted states for ALL of the pages on the single domain/site? basically the entire site system is template driven and essentially 'includes' the content for each individual page within the template.

simple, but it works. if i can get the cookies to save the state across all of the pages, it would be ideal...

thanx in advance,
mike w
gdgi

gekido
07-01-2005, 10:35 PM
brief followup - yes i know i could use a side-frame to do the same thing, via the 'outlook' type script on dd, but i'd rather have it all in a single page - the graphic design layout wouldn't look as nice with a sideframe...

thanx

matg
07-18-2005, 10:47 AM
Gekido,

did you solve this as I'm trying to do the same.

Also when you quit your browser the cookie is deleted so when you go back to your site all the items are minimized again!

Would appreciate any help.

Thanks

matg
07-18-2005, 10:55 AM
Gekido,

I found from using your updated script that if you also remove.location then it works across all pages served, however when you quit your browser the cookie is still deleted and therefore all items are minimized when you open it again.

this is my altered part of the script:

function getselectedItem(){
//if (get_cookie(window.location.pathname) != ""){
//selectedItem=get_cookie(window.location.pathname)
if (get_cookie(window) != ""){
selectedItem=get_cookie(window)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="block")
selectedItem+=ccollect[inc].id+"|"
inc++
}

//document.cookie=window.location.pathname+"="+selectedItem
document.cookie=window+"="+selectedItem
}

function do_onload(){
//uniqueidn=window.location.pathname+"firsttimeload"
uniqueidn=window+"firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && ccollect.length>0){
document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0"
firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
if (!firsttimeload)
revivecontent()
}
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}

jscheuer1
07-18-2005, 11:59 AM
Cookie deletion on browser close is an option that can be set in the browser.

matg
07-18-2005, 12:35 PM
Yes I know, but thats not the problem the browser is set right, the cookie still gets deleted.

The same happends for the example on this site, try it. Open item 3, quit your broweser, reopen and then look at the page again, item 3 will be closed.

jscheuer1
07-18-2005, 12:46 PM
Whoa, isn't that a good thing? Why would you want the switch state to last across browser sessions?

matg
07-18-2005, 01:26 PM
Im implementing this script as a way of customising a left hand side bar.

The idea is a user chooses from the 7 sections which ones they want minimized or maximized. I need the cookie to be retained as once they have set this once when they return to the site the sections they have chosen to have minimized or maximized are remembered. Think of it as away of having selected portlets but based on a cookie rather than a user ID and login.

Hope that makes sense.

jscheuer1
07-18-2005, 01:59 PM
Well, I'm new to this but, I'd try changing:

document.cookie=window.location.pathname+"="+selectedItemto:

var expdate = new Date("January 1, 3000");
document.cookie=window.location.pathname+"="+selectedItem+";expires="+expdate.toGMTString();

matg
07-18-2005, 04:24 PM
Thanks for the help, but thats not doing much at all.

jscheuer1
07-19-2005, 03:34 AM
Google cookies then and see what code is used.

Added Later:

I've really looked into this more and there is something about the code that will not allow persistence across sessions in the usual manner. Either that or it is a situation that once you set a cookie it interferes with subsequent cookie behavior. I've seen that happen before. One reason I detest cookies. Sorry I couldn't be of more help.

matg
07-19-2005, 08:28 AM
thanks for all your help, looks like its no going to work

jscheuer1
07-19-2005, 08:55 AM
That's the maddening thing about it. It should and will work if we can find the right way to go about it. Maybe once I let it go for a few days . . . Also, someone else may come up with a solution. I'm never opposed to that. If that happens, I may learn something too.