We help IT Professionals succeed at work.

cfif URL = index then do this

Zac
Zac asked
on
Medium Priority
1,544 Views
Last Modified: 2012-05-06
hi all, what i would like to say is this... IF the URL is index.cfm THEN do this. i'm sort of making the code up as i go along because i really dont know much about coldfusion. am i way off?
<cfif IsDefined ('URL.index.cfm')>
<li class="active"><a href="index.cfm"><span>home</span></a></li>
<cfelse>
<li class="hover"><a href="index.cfm"><span>home</span></a></li>
</cfif>

Open in new window

Comment
Watch Question

erikTsomikSystem Architect, CF programmer
Commented:
can you try something like this
<cfif FindNoCase (cgi.script_name,index.cfm) gt 0>
<li class="active"><a href="index.cfm"><span>home</span></a></li>
<cfelse>
<li class="hover"><a href="index.cfm"><span>home</span></a></li>
</cfif>

or pass additional parametr with url . Like this index.cfm?page=home
and then
<cfif URL.page EQ 'home'>
<li class="active"><a href="index.cfm"><span>home</span></a></li>
<cfelse>
<li class="hover"><a href="index.cfm"><span>home</span></a></li>
</cfif>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Commented:
<cfif IsDefined ('URL.index.cfm')>

probably won't work the way you think

url variables are set by the calling url ie: index.cfm?myVar=1

so

<cfif IsDefined ('URL.myVar')>
the variable is #url.myvar#
</cfif>

you might want to do something like this

<cfparam name="url.classType" default="">
<cfif URL.classType eq "active">
<li class="active"><a href="index.cfm"><span>home</span></a></li>
<a href="index.cfm?classType=active">Set Hover</a>
<cfelse>
<li class="hover"><a href="index.cfm"><span>home</span></a></li>

<a href="index.cfm?classType=active">Set Active</a>

</cfif>

you use cfparam to ensure the variable exists when it hasn't been passed in the url


Commented:
<cfif find(cgi.script_name, 'index.cfm')>

Commented:
which is what erikTsomik posted, but he missed the quotes around index.cfm
Zac

Author

Commented:
hmm, not quite working yet....

i'm working on this one:

<cfif FindNoCase (cgi.script_name,'index.cfm') gt 0>
<li class="active"><a href="index.cfm"><span>home</span></a></li>
<cfelse>
<li class="hover"><a href="index.cfm"><span>home</span></a></li>
</cfif>

it doesnt seem to have any effect.


from lookig at this one:

<cfparam name="url.classType" default="">
<cfif URL.classType eq "active">
<li class="active"><a href="index.cfm"><span>home</span></a></li>
<a href="index.cfm?classType=active">Set Hover</a>
<cfelse>
<li class="hover"><a href="index.cfm"><span>home</span></a></li>

<a href="index.cfm?classType=active">Set Active</a>

</cfif>



what is this extra line for?  <a href="index.cfm?classType=active">Set Active</a> ive tried it with and without this but i cant get it to work.

erikTsomikSystem Architect, CF programmer

Commented:
you must pass classType  with the url
Zac

Author

Commented:
?

index.cfm is the landing page so i dont kow how to get something into the url before something has been pressed/clicked on the page. also what is classtype?
erikTsomikSystem Architect, CF programmer

Commented:
pass as url variable
please post your code
Zac

Author

Commented:
ok so hee is the .cfm page & the CSS below it.

you'll see that all other links get there values from a query. its only the home button i.e. the index.cfm page that is constant.
<ul>
<cfif FindNoCase (cgi.script_name,'index.cfm') gt 0>
<li class="active"><a href="index.cfm"><span>home</span></a></li>
<cfelse>
<li class="hover"><a href="index.cfm"><span>home</span></a></li>
</cfif>
 
<cfoutput query="getCat">
<cfif getCat.category_ID EQ URL.category>
<!--- active tab --->
    <li class="active"><a href="results.cfm?category=#getCat.category_ID#&amp;producttype=mainline&amp;imagesonly=1"><SPAN>#getCat.catName#</SPAN></a></li>
<cfelse>
<!--- default tab --->
    <li><a href="results.cfm?category=#getCat.category_ID#&amp;producttype=mainline&amp;imagesonly=1"><span>#getCat.catName#</span></a></li>
</cfif>
</cfoutput>
</ul>
 
 
 
css:
#navmenu ul {
        margin:0;
        padding:0;
        list-style:none;
        width:100%;
        float:left;
        
}
 
#navmenu ul li {
        float:left;
                
}
 
#navmenu li a {
        text-decoration: none;
        color:#ffffff;
        float:left;
        background:url(../images/gifs/bluetab_right.gif) no-repeat top right;
}
 
#navmenu li a span {
        padding:0 1em;
        line-height:2.1em;
        background:url(../images/gifs/bluetab_left.gif) no-repeat top left;
        float:left;
}
 
#navmenu ul a:hover,
#navmenu ul li.active a {
        color: #FFFFFF;
        background:url(../images/gifs/redtab_right_hover.gif) no-repeat top right;
}
 
#navmenu ul a:hover span,
#navmenu ul li.active a span {
        background:url(../images/gifs/redtab_left_hover.gif) no-repeat top left;
}

Open in new window

CERTIFIED EXPERT

Commented:
is that working for you?

I suggest modifying the second part as shown below.  The isDefined() function protects you from an error in case there is no category= on the URL.

Also, only the li with the class needs to be in the <CFIF>  that way you don't have to keep the two links in sync - as there is only one link statement now.
<cfoutput query="getCat">
  <cfif isDefined("URL.category") and URL.category is getCat.category_ID>
    <li class="active">  <!--- active tab --->
  <cfelse>
    <li> <!--- default tab --->
  </cfif>
  <a href="results.cfm?category=#getCat.category_ID#&amp;producttype=mainline&amp;imagesonly=1"><span>#getCat.catName#</span></a>
  </li>
</cfoutput>

Open in new window

Zac

Author

Commented:
i'm reluctant to change the second bit because that's the only bit that's working at the moment. its the first bit that i don't seem able to get going. you'd think that would be the easiest bit.

all i want to do is IF we are on the index.cfm page THEN display the ACTIVE state.
CERTIFIED EXPERT
Commented:
Ok, regarding the second bit... test your code by removing the category from your URL, make sure you have that covered.  You don't want to crash and burn.

Regarding the first part...

This should work for you...
<cfif listLast(cgi.script_name,"/\") is "index.cfm">
   <li class="active">
<cfelse>
   <li class="hover">
</cfif>
  <a href="index.cfm"><span>home</span></a>
</li>

Also, the problem with the code you have is that you have the two parameters of FindNoCase backwards.   findNoCase(substring, string)
so it should be..

  FindNoCase ("index.cfm",cgi.script_name)

But before we settle, let me ask the question.   What happens if you're on index.cfm in a subfolder?   /index.cfm  or  /myAccount/index.cfm  
Are you only wanting to match the top level or any level?

 Ok, once your first part works, pop-in the code for the second part.  :)

Zac

Author

Commented:
ok IT WORKS!!!!!!

i have pasted all the code below. but it all seems to be working perfectly.

thanks very much for your help.

ref your question about subfolders, this is not important for me on this project, only top level.
<ul>
<cfif listLast(cgi.script_name,"/\") is "index.cfm">
   <li class="active">
<cfelse>
   <li class="hover">
</cfif>
  <a href="index.cfm"><span>home</span></a>
</li>
 
<cfoutput query="getCat">
<cfif isDefined("URL.category") and URL.category is getCat.category_ID>
<!--- active tab --->
    <li class="active"><a href="results.cfm?category=#getCat.category_ID#&amp;producttype=mainline&amp;imagesonly=1"><SPAN>#getCat.catName#</SPAN></a></li>
<cfelse>
<!--- default tab --->
    <li><a href="results.cfm?category=#getCat.category_ID#&amp;producttype=mainline&amp;imagesonly=1"><span>#getCat.catName#</span></a></li>
</cfif>
</cfoutput>
</ul>

Open in new window

CERTIFIED EXPERT

Commented:
That was for future reference, in case you add subfolders.

This will match the index.cfm file of any level (including within subfolders)

<cfif listLast(cgi.script_name,"/\") is "index.cfm">


This will match only the top level index.cfm

<cfif cgi.script_name is "/index.cfm">


You can leave it as is, no harm...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.