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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This works but is there a better way?

Status
Not open for further replies.

mmerlinn

Programmer
May 20, 2005
748
US
This code works when the webpage is on a Mac, but fails when on a PC. Changing the fourth line to work on a PC causes it to fail on a Mac. The solution of course is to test for which platform is being used then choose the correct format.

This code also works as expected when the same unchanged page is hosted by Geocities, Tripod, and Bravehost. And it will probably work ok on many other sites. However, it does not always work in all cases, so I was wondering if there is an easy way to alter this code so that it will work in most instances?

Or is there a completely different way to do this better?

[tt]
<SCRIPT LANGUAGE='javascript'><!--
var a = "host";
eval(a + " = location." + a);
a = (host == "") ? "/MHD/Desktop Folder/GeoCity" : "";
a = (host == " ? "/Athens/Thebes/6602" : a;
var bref = location.href.substring(0, 7) + host + a + "/";

document.writeln();
document.writeln("<BASE HREF='" + bref + "'>");
// --></SCRIPT>
[/tt]



mmerlinn

"Political correctness is the BADGE of a COWARD!"
 
I don't have access to a Mac to test this, but I'd write your example this way.
Code:
var host = location.host, a = '';

switch (host)
  {
  case "":
    {
    a = "/MHD/Desktop Folder/GeoCity";
    break;
    }
  case "[URL unfurl="true"]www.geocities.com":[/URL]
    {
    a = "/Athens/Thebes/6602";
    break;
    }
  }

var bref = location.href.substring(0, 7) + host + a + "/";

document.write("<BASE HREF='" + bref + "'>");

I prefer to stay away from eval() calls and there is almost always a clearer way of doing things. The ternary operator isn't really necessary here, either, and can be less clear than a slightly more verbose if or switch statement. Since you make multiple comparisons to the value in host, I'd say that the switch statement is the logical choice, and is clearer for adding other possibilities to.

Lee
 

Lee:

Your code is definitely much clearer. And if it works it will dispose of eval() which is preferable to me also.

For this purpose I do not like the if statement at all. However, I have never been able to get the switch statement to work in any context, so I opted for the construction used above.

I will try out your code and see if I can get it to work. As you note, it does have the advantages of readability and changeability.

I originally wrote the above code about 6 years ago and have dropped it into several hundred pages with extremely few problems. However, I am now writing a FoxPro program to automate generation of webpages. I want to make sure that when I automatically drop this code in that I won't be creating problems on hundreds of pages. By the same token I don't want to hard code the <BASE HREF .. > statement into pages that result in pages that are not portable.

Another question though. Since location.host returns "" on both Mac and PC computers, how can I differentiate between the two? It would be nice to be able to develop pages on either machine without constantly having to change this code. It is even nicer when I don't need to worry about the end location of whatever page I am working on.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top