Choosing a URL

H

Howard Kaikow

I'd like to be able to include something like the following for many links
in my HTML documents:

<p><b><a
href=""ChooseURL("http://www.standards.com/index.html?Sorting","file://Sorti
ng/SortPerformanceComparison-Description.html")"">Bagels or
Lox??</a></b></p>

Where ChooseURL is:

function ChooseURL(Global, Local)
{
{
if (exits(Global))
{
return Global;
}
else
{
return Local;
}
}
}

The goal is to have an href link to the file on the web, when connected to
the internet, and, if not connected to the internet, to a local copy of the
file. Note that the Global uRL may/will require processing by another
JavaScript.

How can this be done?
 
D

David Mark

I'd like to be able to include something like the following for many links
in my HTML documents:

<p><b><a
href=""ChooseURL("http://www.standards.com/index.html?Sorting","file://Sorti
ng/SortPerformanceComparison-Description.html")"">Bagels or
Lox??</a></b></p>

More like:

<a href="http://www.standards.com/index.html?Sorting"
onclick="ChooseURL('http://www.standards.com/index.html?
Sorting','file:///Sorting/SortPerformanceComparison-
Description.html');return false">Bagels or Lox?</a>

And then you want to check the protocol in the ChooseURL function and
navigate accordingly. But why?
Where ChooseURL is:

function ChooseURL(Global, Local)
{
{
if (exits(Global))
{
return Global;
}
else
{
return Local;
}
}
}

I assume this is an example of what you think the function should do.
And I don't see a function called "exits." Regardless, returning a
URI won't do anything. You will have to set the location (see the
window.location object.)
The goal is to have an href link to the file on the web, when connected to
the internet, and, if not connected to the internet, to a local copy of the
file. Note that the Global uRL may/will require processing by another
JavaScript.

You mean that you want to maintain two different structures? One for
your local copy and one for the Web? By "processing by JavaScript", I
assume you mean that the Web version is generated by CGI on the
server.

What are you trying to accomplish with this?
 
H

Howard Kaikow

I am trying to pass 2 URLs to a function.
If the first does not exist, then the 2nd would be used.

"exits" should have been "exists", but I ow see that is server-side method.
need to do the deed client side.

I guess that I do not understand error trapping in javascript.
 
D

David Mark

I am trying to pass 2 URLs to a function.
If the first does not exist, then the 2nd would be used.

You don't need to worry about a case with a single URI. Put that URI
in the href attribute of the link and leave off the onclick attribute
entirely.
"exits" should have been "exists", but I ow see that is server-side method.
need to do the deed client side.

I guess that I do not understand error trapping in javascript.

I don't see what error trapping has to do with this.
 
D

David Mark

Currently, I have the following.
Seems to me that the href is not kerect.

Do you mean the missing /j/ at the front of index.html? That is new.
<p><A href="http://www.standards.com/index.html?Sorting"
onclick="ChooseURL('http://www.standards.com/jindex.html?Sorting','file:///
Sorting/SortPerformanceComparison-Description.html');return
false">Bagels or Lox?</a></p>

using

function ChooseURL(Global, Local)
{
try
{
location.replace(Global);}

catch(er)
{
location.replace(Local);

Don't use location.replace for this as it does not create a history
entry (back button won't work as expected.) Set location.href.
 
D

David Mark

Currently, I have the following.
Seems to me that the href is not kerect.

<p><A href="http://www.standards.com/index.html?Sorting"
onclick="ChooseURL('http://www.standards.com/jindex.html?Sorting','file:///
Sorting/SortPerformanceComparison-Description.html');return
false">Bagels or Lox?</a></p>

using

function ChooseURL(Global, Local)
{
try
{
location.replace(Global);}

catch(er)
{
location.replace(Local);
And why are you using try/catch? If you are determined to pass
invalid parameters to this function (ie less than two), test for
undefined parameters (or empty strings or whatever it is you plan to
pass.) As I mentioned previously, you don't need to do any of this if
you have only one URI for the link.
 
H

Howard Kaikow

David Mark said:
Do you mean the missing /j/ at the front of index.html? That is new.

I did that intentionally so that URL would fail.
Don't use location.replace for this as it does not create a history
entry (back button won't work as expected.) Set location.href.

Thanx.

I fergot about that.
 
H

Howard Kaikow

David Mark said:
And why are you using try/catch? If you are determined to pass
invalid parameters to this function (ie less than two), test for
undefined parameters (or empty strings or whatever it is you plan to
pass.) As I mentioned previously, you don't need to do any of this if
you have only one URI for the link.

I do not understand.

1 of the 2 links will always be valid, but I will not know which.

I put an invalid link in the example for testing to see whether the 2nd link
would be used.

I still do not understand how to include the function call and have text
displayed as a link.
 
D

David Mark

I do not understand.

1 of the 2 links will always be valid, but I will not know which.

I put an invalid link in the example for testing to see whether the 2nd link
would be used.

You mean you added that /j/ to see if a 404 error would cause a
location.replace to fail? It won't. You can't test if the page
exists without getting into Ajax requests. What exactly are you
trying to accomplish with this? I thought you just wanted to choose
between a local or remote link.

By invalid parameters, I thought you meant you would sometimes have
only one URI for a link and hence there would be no choice for the
function to make.
I still do not understand how to include the function call and have text
displayed as a link.

I don't understand what that means.
 
H

Howard Kaikow

I want to have a link that displays text, say, "Bagels & Lox".
When clicking on the link, I want hte link to go to ans alternate link, if
the first link does not exist.

The intent is that the first link will be a http link and the second will
will be a file link.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top