Click to See Complete Forum and Search --> : entry URL


chook
05-05-2003, 05:37 AM
Any ideas anyone?

There are two possible URL's which users can use to enter my page. I want to set up one link on my page, which does two different things, depending on which URL the user initially entered through.

If the user entered via URL "A" then the link should say "Click ME!" and on click it should do this:
"javascript:void(top.location='https://www.blah.blah.cgi')"

If the user entered via URL "B" then the link should also say "Click ME!" but this time on click it should do this:
alert("Here is some text");

I've found this bit of code already which executes differently depending on the entry URL, but can't customise it for the two different events I want to happen onclick:

<!-- This script and many more are available online from -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
if (location.href.indexOf("come.to") != -1){
document.write("Thanks for visiting!");
}
else {
document.write("Our new address is http://come.to/......");
}
// End -->
</SCRIPT>

gil davis
05-05-2003, 07:10 AM
<script>
var urlA = "someURL";
var urlB = "someOtherURL";
var linkAction = "return false";
if (window.top.location.href.indexOf(urlA) != -1)
{linkAction = "window.top.location = 'https://www.blah.blah.cgi';return false";}
if (window.top.location.href.indexOf(urlB) != -1)
{linkAction = "alert('Here is some text');return false";}
document.write('<a href="#" onclick="' + linkAction + '">Click ME!</a>');
</script>

chook
05-05-2003, 07:32 PM
Thanks Gil.That's a big help.

Just a twist to the original query:
What would you do if there was a third entry URL (say urlA2) which was a different URL to the other two but should behave in the same way as urlA onclick.

Do I have to add a urlA2 variable and then repeat this line:

if (window.top.location.href.indexOf(urlA) != -1)
{linkAction = "window.top.location = 'https://www.blah.blah.cgi';return false";}

OR

is there some way of saying:
var urlA = "someURL" AND "thisURL"

gil davis
05-06-2003, 05:54 AM
Originally posted by chook
What would you do if there was a third entry URL (say urlA2) which was a different URL to the other two but should behave in the same way as urlA onclick.var urlA2 = "thisURL";
...
if ((window.top.location.href.indexOf(urlA) != -1) || (window.top.location.href.indexOf(urlA2) != -1))
is there some way of saying:
var urlA = "someURL" AND "thisURL"You can say that using "&&" instead of "AND", but it won't do what you might think it will do.

chook
05-07-2003, 03:45 AM
Delightful!

Thanks Gil - it works a treat.