Scroll To Top

Enter a query to search our site. Note that you can use "*" and "?" as wildcards. Enclosing more than one word in double quotes ("CSS Layout") will search for the exact phrase.

PVII Knowledge Base

1. Select a product ...
2. Select category ...
3. Search the Knowledge Base...

Question #277

Can I set the Tree Menu Magic 2 system to automatically open to a specific menu item based on an URL parameter set dynamically?

The Answer

Prepared Dec. 2009 by Gerry Jacobsen, PVII

Heres's a special Tree Menu Magic 2 (TMM2) function that will check for a specific URL parameter in the page address and look for a match in the menu links, and then current mark and open the menu accordingly.

Here's how to deploy this tweak:

1. Open the p7TMMscripts.js file and scroll down to the end of the file, make a new line and then Copy & Paste this new function into that line:

function P7_TMMopenUrlParam(dv,param){
var i,h,s,vV,tD,k=-1,pp;
tD=document.getElementById(dv);
if(tD) {
h=document.location.search;
if(h){
h=h.replace('?','');
s=h.split(/[=&]/g);
if(s&&s.length) {
for(i=0;i<s.length;i+=2) {
if(s[i]==param&&s[i+1]) {
vV=param+'='+s[i+1];
break;
}
}
}
}
if(vV){
tA=tD.getElementsByTagName('A');
for(i=0;i<tA.length;i++){
if(tA[i].getAttribute('href')!='#'&&tA[i].search&&tA[i].search.indexOf(vV)>-1){
k=i;
break;
}
}
}
if(k>-1){
pp=tA[k].parentNode;
while(pp){
if(pp.tagName&&pp.tagName=='LI'){
P7_TMMsetClass(pp,'li_current_mark');
a=pp.getElementsByTagName('A');
if(a&&a[0]){
P7_TMMsetClass(a[0],'current_mark');
if(a[0].hasImg){
im=a[0].getElementsByTagName('IMG')[0];
im.mark=true;
im.src=im.p7imgswap[3];
}
if(a[0].tmmsub){
P7_TMMopen(a[0],1);
}
}
}
else{
if(pp==tD){
break;
}
}
pp=pp.parentNode;
}
}
}
}

2. Now we'll add activate this to run automatically when the page loads. Locate this code block near the end of the P7_TMMinit() function, around line 227:

  if(tM.p7opt[9]>-1){
P7_TMMall(tM.id,'open',tM.p7opt[9]);
}
}
}
p7TMMa=true;
}

3. Add a new line after the first } and Copy & Paste this into that new line:

P7_TMMopenUrlParam(tM.id,'deptid');

so that it now looks like this:

   if(tM.p7opt[9]>-1){
P7_TMMall(tM.id,'open',tM.p7opt[9]);
}
P7_TMMopenUrlParam(tM.id,'myParam');
}
}
p7TMMa=true;
}

4. Change the 'myParam' text to reflect the actual parameter you wish to match, for example, if you want the system to look for deptID parameter it should look like this:

    P7_TMMopenUrlParam(tM.id,'deptID');

4. Save and close the file.

When the page first loads the TMM2 system will now look at the page's URL parameters and if it finds a 'deptID' parameter it will compare the 'deptID' value in the page URL to the menu links. If it finds a menu link with a 'deptID' parameter and matching value it will current mark that link and then open the sub menu and any parents.

So, if the detail product page address is:
http://mySite.com/product.php?deptID=263&prodID=1022

the system will match to this link in the menu:
<li><a href="/department.php?deptID=263>Department 263</a></li>

Back to the questions list