Choose code to run based on incoming URL

H

Howard Kaikow

Can I use a bookmark to selectively choose the code to be run?



For example. Suppose we have the following in an HTML file:



<html>

<head>

<script language="JavaScript"><!--

location.href = 'HKlinks.html#B'

//--></script>

<noscript>

<meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">

</noscript>

</head>

</html>



And hklinks.html has:



<html>

<head>

<a name="A"><script language="JavaScript"><!--

location.href = 'HowardKaikow.html'

//--></script></a>

<a name="B"><script language="JavaScript"><!--

location.href = HowardKaikowServices.html'

//--></script></a>

<noscript>

<meta http-equiv="Refresh" content="3;url=index.html">

</noscript>

</head>

</html>



I want to choose which javascript to run based on the incoming link.

I tried onload.target to choose, but I think I screwed up the sintax.
 
M

McKirahan

Howard Kaikow said:
Can I use a bookmark to selectively choose the code to be run?



For example. Suppose we have the following in an HTML file:



<html>

<head>

<script language="JavaScript"><!--

location.href = 'HKlinks.html#B'

//--></script>

<noscript>

<meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">

</noscript>

</head>

</html>



And hklinks.html has:



<html>

<head>

<a name="A"><script language="JavaScript"><!--

location.href = 'HowardKaikow.html'

//--></script></a>

<a name="B"><script language="JavaScript"><!--

location.href = HowardKaikowServices.html'

//--></script></a>

<noscript>

<meta http-equiv="Refresh" content="3;url=index.html">

</noscript>

</head>

</html>



I want to choose which javascript to run based on the incoming link.

I tried onload.target to choose, but I think I screwed up the sintax.

How about the following instead?

<html>
<head>
<script type="text/javascript">
location.href = "HKlinks.html?B";
</script>
<noscript>
<meta http-equiv="Refresh" content="3;url=HowardKaikowServices.html">
</noscript>
</head>
</html>


<html>
<head>
<script type="text/javascript">
var loc = location.search;
if (loc == "?A") location.href = "HowardKaikow.html";
if (loc == "?B") location.href = "HowardKaikowServices.html";
</script>
<noscript>
<meta http-equiv="Refresh" content="3;url=index.html">
</noscript>
</head>
</html>
 
M

Michael Winter

[snip]
I want to choose which javascript to run based on the incoming link.
[snip]

How about the following instead?

Since we're only talking about redirection, why not do it the proper way.
With the server.

[snip]

Mike


Please trim quotes!
 
H

Howard Kaikow

Michael Winter said:
[snip]
I want to choose which javascript to run based on the incoming link.
[snip]

How about the following instead?

Since we're only talking about redirection, why not do it the proper way.
With the server.

Because my ISP limits what we can do and even then discourages server side
stuff.

I could use a custom 404 file, but I want to try javascript first.
[snip]

Mike


Please trim quotes!
 
H

Howard Kaikow

How do I modify the code to handle both ? and # as bookmark delimiters?

For example, in:
<html>
<head>
<p><a href="HKlinks.html?B">B?</a></p>
<p><a href="HKlinks.html?A">A?</a></p>
<p><a href="HKlinks.html#B">B#</a></p>
<p><a href="HKlinks.html#A">A#</a></p>
</head>
</html>

Only the links using ? work.
I commonly see # used instead f ? to separate the bookmark.


What modifications are needed in the following HKlinks.html?
<html>
<head>
<script type="text/javascript"><!--
var loc=location.search;
if(loc=="?A")location.href="HowardKaikow.html";
if(loc=="?B")location.href="HowardKaikowServices.html";
//--></script>
<noscript>
<meta http-equiv="Refresh" content="3;url=index.html">
</noscript>
</head>
</html>
 
M

McKirahan

Howard Kaikow said:
How do I modify the code to handle both ? and # as bookmark delimiters?

For example, in:
<html>
<head>
<p><a href="HKlinks.html?B">B?</a></p>
<p><a href="HKlinks.html?A">A?</a></p>
<p><a href="HKlinks.html#B">B#</a></p>
<p><a href="HKlinks.html#A">A#</a></p>
</head>
</html>

Only the links using ? work.
I commonly see # used instead f ? to separate the bookmark.


What modifications are needed in the following HKlinks.html?
<html>
<head>
<script type="text/javascript"><!--
var loc=location.search;
if(loc=="?A")location.href="HowardKaikow.html";
if(loc=="?B")location.href="HowardKaikowServices.html";
//--></script>
<noscript>
<meta http-equiv="Refresh" content="3;url=index.html">
</noscript>
</head>
</html>

First, you should no longer use "<!--" and "--> within <script> tags.

Second, your <a> tags go inside the <body> not the <head> section.

Thirdly, you have a solution with "?" why try to use "#"?

"#" are used for bookmarks (as you know).

"?" indicates a QueryString which can be interrogated by the called page.
 
H

Howard Kaikow

Because when I gove somebody a URL, I usually give it as a web page and,
maybe, a bookmark on that web page, which is also how a lot of folkes are
used to seeing a URL.

I'll have no control whether they use a ? or a #.
Here's what I have in the .js file, but the part about the # is doing what I
need.

<html>
<head>
<script type="text/javascript", SRC ="links.js"></script>
<noscript>
<HR>
<A href="index.html">The URL you used requires the use of JavaScript. If you
wish to use the URL,
please enable support for Javascript in your browser.
You will shortly be taken to http://www.standards.com/index.html, or you can
click here now.</A>
<HR>
<meta http-equiv="Refresh" content="0;url=index.html">
</noscript>
</head>
</html>

The following is currently the link.js file.

var loc=location.search.toUpperCase();
if((loc=="?A")||(loc=="#A")){
location.href="HowardKaikow.html";
}
else if((loc=="?B")||(loc=="#B")){
location.href="HowardKaikowServices.html";
}
else{
alert(location.href + " is not a valid URL.\n" +
"You will be taken to http://www.standards.com/index.html.");
location.href="index.html";
}
 
H

Howard Kaikow

Howard Kaikow said:
Here's what I have in the .js file, but the part about the # is doing what I
need.

I meant to say "but the part about the # NOT is doing what I need".
 
M

McKirahan

Howard Kaikow said:
Because when I gove somebody a URL, I usually give it as a web page and,
maybe, a bookmark on that web page, which is also how a lot of folkes are
used to seeing a URL.

I'll have no control whether they use a ? or a #.
Here's what I have in the .js file, but the part about the # is doing what I
need.

<html>
<head>
<script type="text/javascript", SRC ="links.js"></script>
<noscript>
<HR>
<A href="index.html">The URL you used requires the use of JavaScript. If you
wish to use the URL,
please enable support for Javascript in your browser.
You will shortly be taken to http://www.standards.com/index.html, or you can
click here now.</A>
<HR>
<meta http-equiv="Refresh" content="0;url=index.html">
</noscript>
</head>
</html>

The following is currently the link.js file.

var loc=location.search.toUpperCase();
if((loc=="?A")||(loc=="#A")){
location.href="HowardKaikow.html";
}
else if((loc=="?B")||(loc=="#B")){
location.href="HowardKaikowServices.html";
}
else{
alert(location.href + " is not a valid URL.\n" +
"You will be taken to http://www.standards.com/index.html.");
location.href="index.html";
}

First, you can change
if((loc=="?A")||(loc=="#A")){
to
if(loc=="?A"||loc=="#A"){
if you want.

Second, what do you mean that you "... have no control whether they use a ?
or a #" after you state "... when I gove [sic] somebody a URL". If you give
them a URL it has what ever suffix you include; such as "?A".

Thirdly, it still isn't clear what your really trying to accomplish (big
picture).

It isn't clear why you need to use "#"; is it that you want to jump "into" a
page and not just "to" a page?
 
H

Howard Kaikow

I figured out how to handle both ? and #.

I also stumbled upon an easier way get help.
By putting the code in a .js file, I can right click and open in VS .NET
..2003, which gives me the JScript help, etc.
 
H

Howard Kaikow

McKirahan said:
Second, what do you mean that you "... have no control whether they use a ?
or a #" after you state "... when I gove [sic] somebody a URL". If you give
them a URL it has what ever suffix you include; such as "?A".

Yes, but there's no reason to not also accept "#A".
I figured out how to do it, see below.
Thirdly, it still isn't clear what your really trying to accomplish (big
picture).

I have a very narrow scope in mind.

I have a web site that has a number of pages I often reference in
newsgroup/web forum postings.
For example, a popular one is
http://www.standards.com/OhMyWord/WordVBABooks.htm, another recent one has
been
http://www.standards.com/OhMyWord/VBABugsFeatures/FailureToDeleteToolbar.html.

To make it easier for me to remember URLs to give to others and to
facilitate changing the structure of the web site, I decided that a "links"
page would be the right way to go. Server side links would be better, but I
do not yet know how to do that. Should not be difficult to make a custom 404
page, but I do not yet know how. I won't learn PHP just for this purpose.

I would always give URLs to the links page and the link would then be
redirected to the real page, so the above URLs would become, say:

http://www.standards.com/links.html#WordVBABooks and
http://www.standards.com/links.html#FailureToDeleteToolbar

Those URLs would be much easier to remember.

Of course, those URLs would only work if the user had Javascript enabled.
Otherwise, I will redirect to http://www.standards.com/.

The hard coded URL could still be used, but I won't guarantee those would
not change.
It isn't clear why you need to use "#"; is it that you want to jump "into" a
page and not just "to" a page?

All the URLs will be pointing to an anchor in the same page, each of which
will be redirected to the real URL.
If both ? and # work, then I see no reason why my script should not handle
both.

function CompareBookmarks(bmkTarget)
{ var temp = bmkTarget.toLowerCase();
return (locSearch=="?" + temp)||(locHash=="#" + temp);
}
var locHash=location.hash.toLowerCase();
var locSearch=location.search.toLowerCase();
if(CompareBookmarks("HowardKaikow")){
location.href="HowardKaikow.html";
}
else if(CompareBookmarks("HowardKaikowServices")){
location.href="HowardKaikowServices.html";
}
else if(CompareBookmarks("SortPerformanceComparison")){
location.href="Sorting/SortPerformanceComparison-Description.html";
}
else if(CompareBookmarks("ThisandThat")){
location.href="index.html#ThisandThat";
}
else if(CompareBookmarks("CopyFileToPrinter")){
location.href="ThisAndThat/CopyFileToPrinter.html";
}
else{
alert(location.href + " is not a valid URL.\n" +
"You will be taken to http://www.standards.com/index.html.");
location.href="index.html";
}
 
M

McKirahan

Howard Kaikow said:
I figured out how to handle both ? and #.

I also stumbled upon an easier way get help.
By putting the code in a .js file, I can right click and open in VS .NET
.2003, which gives me the JScript help, etc.


I'm glad that you figured it out...

The reason "#" is handled differently than "?" is because "?" denotes a
QueryString which is returned via location.search.
 
H

Howard Kaikow

I solved the back button problem by using the replace method of the location
object.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top