PDA

View Full Version : Multiple DD Scripts Conflict



mgisonno
08-04-2006, 02:38 AM
1) Script Title: Swith Content II and Slashdot Menu

2) Script URL (on DD):

http://www.dynamicdrive.com/dynamicindex17/switchcontent2.htm

and

http://www.dynamicdrive.com/dynamicindex1/slashdot.htm

3) Describe problem: I'm trying to leverage both scripts on one page and they are some how conflicting... The Switch Content II script won't remember the states. If I take away the slashdot menu script, the Switch Content II will remember the states.

It's obviously the way they're both writing the cookies because whichever script i include first, remembers the states and the script below it doesn't work.

Anyone have a quickfix to the way one of them is writing cookies?

boogyman
08-04-2006, 03:13 AM
Sounds to me that you are having trouble with variable(s) / function(s) overlapping. Take a look at the code and if that is the case just rename one of the duplicate names and be sure to make the change throughout the entire script and it should fix your problem

mgisonno
08-04-2006, 03:55 AM
I checked all the functions and variables, but don't see any overlap. I'm not sure how the onload and onunload events work, but I tried also combining both scripts to each pages onload and onunload event. so onload = one; two; ... onunload = one; two;

Still a no go...

mgisonno
08-04-2006, 07:19 PM
anyone?

mburt
08-04-2006, 07:27 PM
Um... Don't try to combine these scripts. They're completely different, I suggest using the slash dot menu, and use cookies (mmm... cookies) to save the info on the user's computer.

mburt
08-04-2006, 07:30 PM
I'm working on how to get a cookie to save the data:)

mgisonno
08-04-2006, 07:32 PM
They are completely different, but I have two very necessary uses for them on the same page.

I'm using the slashdot menu for navigation across multiple pages.

The switch content II to allow the showing and hidding of long pieces of content to my viewers.

One of the business requirements is to have them both remember the users last state, which both of these scripts do flawlessly... by themselves.

I thought someone might have an easy answer as to why they weren't working together and maybe I could debug it somehow...

I'll keep praying I can figure it out.

mgisonno
08-07-2006, 03:40 AM
is that a no?

blm126
08-07-2006, 03:54 AM
The following code is untested, but should work...


<script type="text/javascript">

/***********************************************
* Switch Content script II- &#169; Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use. Last updated April 2nd, 2005.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
var memoryduration="7" //persistence in # of days

var contractsymbol='minus.gif' //Path to image to represent contract state.
var expandsymbol='plus.gif' //Path to image to represent expand state.

/////No need to edit beyond here //////////////////////////

function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

function sweeptoggle(ec){
var inc=0
while (ccollect[inc]){
ccollect[inc].style.display=(ec=="contract")? "none" : ""
inc++
}
revivestatus()
}


function expandcontent(curobj, cid){
if (ccollect.length>0){
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="none")? "none" : ""
curobj.src=(document.getElementById(cid).style.display=="none")? expandsymbol : contractsymbol
}
}

function revivecontent(){
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="none"
}

function revivestatus(){
var inc=0
while (statecollect[inc]){
if (ccollect[inc].style.display=="none")
statecollect[inc].src=expandsymbol
else
statecollect[inc].src=contractsymbol
inc++
}
}

function get_cookie(Name) {
Name = 'switch';
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

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

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="none")
selectedItem+=ccollect[inc].id+"|"
inc++
}
if (get_cookie(window.location.pathname)!=selectedItem){ //only update cookie if current states differ from cookie's
var expireDate = new Date()
expireDate.setDate(expireDate.getDate()+parseInt(memoryduration))
document.cookie = 'switch'+"="+selectedItem+";path=/;expires=" + expireDate.toGMTString()
}
}

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

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate

</script>

mgisonno
08-07-2006, 04:29 AM
That, actually, did not work. I was completely off when I said it was a cookies problem...

The real problem was because both scripts were in seperate .js include files, both trying to call seperate onload and onunload calls, which conflicted...

The solution was to combine both scripts into one .js include file and merge the onload / onunload calls

Just incase anyone wants to use both of these scripts... but that's proabably doubtful... Thanks for the inspiration people!

blm126
08-07-2006, 04:36 AM
well that's what I get for only checking the source of one of the scripts...