Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if...endif statement help needed

Status
Not open for further replies.

MikeCou

Technical User
Apr 23, 2009
10
Can someone please tell me what is wrong in the var part of this code? I'm still trying to come up with a better plan than using absolute paths and this is one option that was suggested to a member of this forum about 3 years ago. I've tried different combinations to no avail.

I am trying to load the subfolder path name(news and show_tell) as a variable and want anything other than that to just follow te path as written. All other html files are in the root folder.

I have no idea what I'm doing. The coding is part of the navigation bar for which is now using absolute paths for everything. I'd like to re-use some of the stuff for other designs if I could and relative paths are much easier to code, not to mention using much less bandwidth.

Here is the code:
************
function LoadNav() {


var path = '';
if (location.href.indexOf('\/show_tell\/') > -1)
{
path = './show_tell';
}
else if (location.href.indexOf('\/news\/') > -1)
{
path = './news';
}



document.write("<div class='navItem2'><a href='./index.html'>Home</a></div>");
document.write("<div class='navItem2'><a href='./meetings.html'>Meetings</a></div>");

document.write( "<ul><li class='folder'>");
document.write( "<div class='navItem2'><div class='expand'><a href='#'>Show 'n Tell</a></div></div>");
document.write( "<ul>");
document.write("<li><a href='' + path + 'show_tell/0209.html'>February 2009</a></li>");
document.write("<li><a href='' + path + 'show_tell/0309.html'>March 2009</a></li>");
document.write("<li><a href='' + path + 'show_tell/0409.html'>April 2009</a></li>");
document.write( "</ul>");
document.write("</li></ul>");


document.write( "<ul><li class='folder'>");
document.write( "<div class='navItem2'><div class='expand'><a href='#'>Newsletters</a></div></div>");
document.write( "<ul>");
document.write("<li><a href='' + path + 'news/march_209_news.html'>March 2009</a></li>");
document.write("<li><a href='' + path + 'news/april_209_news.html'>April 2009</a></li>");
document.write( "</ul>");
document.write("</li></ul>");

document.write("<div class='navItem2'><a href='./Members.html'>Members Directory</a></div>");
document.write("<div class='navItem2'><a href='./events.html'>Special &amp; Upcoming Events</a></div>");

}

**************

Thanks

Mike
 
try by getting rid of the dots in your urls and just reference the pages by going back to the root - this makes the code a lot easier to re-use. I call it an absolute path, but most people call it a root relative path.

so if "news" and "show_tell" are one level from the root, use "/news/myfile.html" or "/show_tell/myfile.html" instead of "./news/..." or simply "news/..."

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Do you mean in the var part of the code or in the document.write part (or both?) ?
 
Do you actually have a file where the paths are ./show_tellshow_tell/0209.html and ./newsshow_tell/0209.html? That's what your code is creating.

If you were to handle the paths with server-side scripting you might be able to see where the problem is more easily. If you want to use client side coding, you should probably add a slash at the end of the path variable values that aren't empty strings.

Lee
 
Do you mean in the var part of the code or in the document.write part (or both?) ?

I mean everywhere...



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Trollacious - No I don't. I have a root folder which has some of the html files and I have 2 subfolders called respectively show_tell and news. The var part of the code is from a thread from this site, 3 years old, which was written as a reply to "JillC" (closed thread216-1199827) that had the same problem I am having. I wish I could get hold of JillC to find out what the final outcome was.

Vicvirk - I will your suggestions and see where that leads.

Can someone tell me at least if the if statement is correct? Shouldn't there be an endif in there somewhere? And if the quotation marks are correct in the document.write section?

Thanks for the help to both of you. It is much appreciated.
 
Your code looks correct syntactically with the if statement. The document.write statements have some errors with the single quotes used where double quotes should be. But the menu won't work like you expect once you get those straightened out because of the way the path variable is added to the links.

For instance, this
Code:
document.write("<li><a href='' + path + 'show_tell/0309.html'>March 2009</a></li>");
will result in one of three paths for this link:
show_tell/0309.html
./show_tellshow_tell/0309.html
./newsshow_tell/0309.html

Since you stated that you have no folder named show_tellshow_tell or newsshow_tell, once you are on a page in the subfolder, your links will all be wrong.

I would use something like:
Code:
function LoadNav() {

var newspath = 'news/';
var showtellpath = 'show_tell/';
var mainpath = '';
if (location.href.indexOf('\/show_tell\/') > -1)
  {
  mainpath = '../';
  showtellpath = '';
  newspath = '../news/';
  }
else if  (location.href.indexOf('\/news\/') > -1)
  {
  mainpath = '../';
  showtellpath = '../show_tell/';
  newspath = '';
  }

document.write("<div class='navItem2'><a href='" + mainpath + "'index.html'>Home</a></div>");
document.write("<div class='navItem2'><a href='" + mainpath + "'meetings.html'>Meetings</a></div>");

document.write( "<ul><li class='folder'>");
document.write( "<div class='navItem2'><div class='expand'><a href='#'>Show 'n Tell</a></div></div>");
document.write( "<ul>");
document.write("<li><a href='" + showtellpath + "0209.html'>February 2009</a></li>");
document.write("<li><a href='" + showtellpath + "0309.html'>March 2009</a></li>");
document.write("<li><a href='" + showtellpath + "0409.html'>April 2009</a></li>");
document.write( "</ul>");
document.write("</li></ul>");  


document.write( "<ul><li class='folder'>");
document.write( "<div class='navItem2'><div class='expand'><a href='#'>Newsletters</a></div></div>");
document.write( "<ul>");
document.write("<li><a href='" + newspath + "march_209_news.html'>March 2009</a></li>");
document.write("<li><a href='" + newspath + "april_209_news.html'>April 2009</a></li>");
document.write( "</ul>");
document.write("</li></ul>");
    
document.write("<div class='navItem2'><a href='" + mainpath + "Members.html'>Members Directory</a></div>");
document.write("<div class='navItem2'><a href='" + mainpath + "events.html'>Special &amp; Upcoming Events</a></div>");
}

Lee
 
[&nbsp;]

In the general format of the IF construction

[blue][tt]if (CONDITION)
{
DO SOMETHING HERE
[red]}[/red]
[/tt][/blue]

the [red]RED[/red] bracket signals the end of the construction. Nothing else is needed.

You probably should go here ( to get a general overview of the javascript language.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Lee. Okay, I see it now. You declared each possibility a variable and assigned from there. I had 1 variable and was trying to assign it values on the fly. Your approach is a lot more logical and now I understand much better.

Merlin. I fully agree with you. I have 4 very prominent icons on my desktop linked directly to the W3C school modules which I use all the time and they have helped me a lot. I'm just having a harder time wrapping my brain around the syntax of javascript. No problems with html, xhtml and css. I also agree with the statement at the bottom of your email. I'm a woodturner and teach the stuff and there is a mile of difference between "how can I do this?" and they hand me the piece to work on, as opposed to stepping up ready to do it for themselves with my guidance. I hope I haven't given the wrong impression. I'm not after a free meal. I've been fighting with this thing for over a week and haven't given up yet trying to understand how and why to do it.

I'm sorry to add that Lee's code still doesn't solve the problem of redundant paths showing up though. I have to dig some more. I'm starting to get a nagging feeling that someone is just about to jump out of the woodwork, laugh at me, and tell me that it's just not possible to do what I want to do using javascript!

Thanks to both of you.
Mike
 
If you use server-side programming (ASP, PHP, ASP.NET) rather than client-side Javascript, you'll be able to see better what the actual problem is.

Otherwise, you can try putting an alert after each document.write and use the same string in the alert that you just wrote to the page.

I've done what you're done using client-side Javascript, but don't recommend it because anyone who has Javascript turned off in their browser won't get any links at all. That's another reason I recommend using server-side programming for this. You can get web servers for free, if you don't have one already set up.

Lee
 
Crap! Talk about dense! That's why some of our club members can't see the nav bar! I completely forgot about that and I do know that some are so paranoid there is no way I'll be able to get them to whisper the word 'javascript', let alone turn it on.

Time to learn ASP I guess. One more new trick for this old dog to learn. I know that the ISP they use charges more to use PHP. (yeah, I can't figure that out either)

Almost 2 weeks of ripping off the last of my hair and now, pffft! it all blows up on me! Well, I've learned a lot and I thank you both for spending the time with me on this.

It's been a slice. Thanks again
Mike
 
If you use JScript ASP (which I prefer over VBScript) you can pretty much use the code you ahve and just change the document.write statements to Response.Write, and put runat="server" in your script tag. You'll also have to get the URL with Request.ServerVariables("URL") instead of location.href. Below is how I'd write the code

Just out of curiosity, did you copy and paste my code into your page, or type it in yourself?

Code:
function LoadNav()
{
var newspath = 'news/';
var showtellpath = 'show_tell/';
var mainpath = '';

var thispage = Request.ServerVariables("URL") + '';
thispage = thispage.toLowerCase();
if (thispage.indexOf('\/show_tell\/') > -1)
  {
  mainpath = '../';
  showtellpath = '';
  newspath = '../news/';
  }
else if (thispage.indexOf('\/news\/') > -1)
  {
  mainpath = '../';
  showtellpath = '../show_tell/';
  newspath = '';
  }
Response.Write("<div class='navItem2'><a href='" + mainpath + "index.html'>Home</a></div>");
Response.Write("<div class='navItem2'><a href='" + mainpath + "meetings.html'>Meetings</a></div>");
Response.Write(" <ul><li class='folder'>");
Response.Write(" <div class='navItem2'><div class='expand'><a href='#'>Show 'n Tell</a></div></div>");
Response.Write(" <ul>");
Response.Write("<li><a href='" + showtellpath + "0209.html'>February 2009</a></li>");
Response.Write("<li><a href='" + showtellpath + "0309.html'>March 2009</a></li>");
Response.Write("<li><a href='" + showtellpath + "0409.html'>April 2009</a></li>");
Response.Write(" </ul>");
Response.Write("</li></ul>");

Response.Write(" <ul><li class='folder'>");
Response.Write(" <div class='navItem2'><div class='expand'><a href='#'>Newsletters</a></div></div>");
Response.Write(" <ul>");
Response.Write("<li><a href='" + newspath + "march_209_news.html'>March 2009</a></li>");
Response.Write("<li><a href='" + newspath + "april_209_news.html'>April 2009</a></li>");
Response.Write(" </ul>");
Response.Write("</li></ul>");
Response.Write("<div class='navItem2'><a href='" + mainpath + "Members.html'>Members Directory</a></div>");
Response.Write("<div class='navItem2'><a href='" + mainpath + "events.html'>Special &amp; Upcoming Events</a></div>");
}

Lee
 
I have some studying to do before I can implement the code the way you describe. I don't have the background knowledge yet to do it. In small easy to follow scripts, I don't usually have too much problem understanding how it works and I can use canned scripts. The minute I don't understand things, I have to stop and study some. In the early days, I would sometimes get into a situation where I couldn't fix something because I didn't understand, but I'm older and wiser now! (older anyway!) I'll read up on ASP and then look at your code again.

I copied the code you wrote and pasted it in place and then moved things around to accomodate my layout and added the rest of the code which I didn't bother to include in the sample above. I'm sure I got it right but I can post the whole script if you want to have a look. A quotation mark or doublequote at the wrong place is easy to do. The code works, but it has the exact result that I got when using just relative paths.

Mike
 
I tried the code I posted above, and the links had the paths they should have. I had an extra single quote in a couple spots in the first version I posted above (the client-side one) and picked that up by viewing the output page source in the server-side version.

If you use

Code:
<script type="text/javascript" runat="server">

for the script tag, then call the function where you want it to display with

Code:
<%
LoadNav();
%>
[code]

it should work find, and you'll have a working example of ASP.  You HAVE to display the page from your web server, though, not as a local HTML file.  Amd make sure the page extension is ASP, not HTM or HTML.

Lee
 
Thanks Lee. I'll give that a go.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top