A script to check the URL of the current page...

T

turbofool

Alright, I know this must be incredibly simple, but my knowledge of
JavaScript is rather basic, and what I've learned in the past I learned
for highly specific situations, so I don't have a clear working
knowledge of it.

I'm looking to make a simple script that will check the location typed
into the user's address bar and if it contains a certain domain name it
will call a function (not sure what yet... maybe add text to the page,
maybe create an alert, maybe an alert and a redirect). The main
concept is to direct people to my new domain name even though I'm
hosting it as a mirror so I can let my old domain name lapse.

So... any suggestions on how to do this? Odds are it's incredibly
simple, but my brain just isn't latching onto the language right now.

Thanks,

TurboFool
 
S

Sanjay

I guess you can do this.

if ("MY_OLD_DOMAIN".indexOf(location.host) != -1 )
{
//redirect to your new domain.
}

Thanks
Sanjay
 
T

Thomas 'PointedEars' Lahn

Sanjay said:
I guess you can do this.

if ("MY_OLD_DOMAIN".indexOf(location.host) != -1 )
{
//redirect to your new domain.
}

It makes more sense the other way around because "MY_OLD_DOMAIN"
is what you are looking for in location.host, not vice-versa:

if (location.host.indexOf("MY_OLD_DOMAIN") != -1)
{
// redirect to your new domain.
}

But remember: <URL:http://www.w3.org/QA/Tips/reback>


PointedEars
 
T

turbofool

Thomas said:
It makes more sense the other way around because "MY_OLD_DOMAIN"
is what you are looking for in location.host, not vice-versa:

if (location.host.indexOf("MY_OLD_DOMAIN") != -1)
{
// redirect to your new domain.
}

But remember: <URL:http://www.w3.org/QA/Tips/reback>

Well, I'd be more likely to make this script either add a text link to
the page notifying them of the change, or create an alert that takes
them to the new page when they hit the OK button.

Any suggestions on the best way to implement the latter, especially a
script that I wouldn't have to custom code the address of for every
page in my site (in otherwords if they access my.old.domain/resume.html
the script would automatically take them to my.new.domain/resume.html
or whatever other address they accessed)? I know I'm getting deep here
and I may just settle for the script you provided (thank you very much,
both of you), but prhaps there's something relatively simple?
 
T

turbofool

Never mind. I added this to the script:

top.location="my.new.domain" + top.location.pathname;

Worked like a charm. Thanks for the assist.
 
V

VK

Any suggestions on the best way to implement the latter, especially a
script that I wouldn't have to custom code the address of for every
page in my site (in otherwords if they access my.old.domain/resume.html
the script would automatically take them to my.new.domain/resume.html
or whatever other address they accessed)? I know I'm getting deep here
and I may just settle for the script you provided (thank you very much,
both of you), but prhaps there's something relatively simple?

<html>
<head>
<title>My site is relocated</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta http-equiv="refresh"
content="3;url=http://www.google.com">
</head>

<body>

<h1>
Hi there!<br>
I just purchased a new domain, so please
update your bookmarks if needed.<br>
If your browser will not redirect you
automatically in a few seconds, please follow
<a href="http://www.google.com">this link</a>
</h1>

</body>
</html>

:-D
 
T

turbofool

Thanks VK, but not that simple. My two domain names are going to
simply mirror my server until it dies because too many different pages
within my site are in search engines or links from other sites, so I
don't want to lose the traffic. That's why I was looking for a script
that could check and see which domain name they accessed it from and
then notify them and redirect. Anyway, I managed with a combination of
Sanjay/Thomas's scripts and a little bit of my own thinking to get it
working. Thanks, all!
 
S

Sanjay

Thomas said:
It makes more sense the other way around because "MY_OLD_DOMAIN"
is what you are looking for in location.host, not vice-versa:

if (location.host.indexOf("MY_OLD_DOMAIN") != -1)
{
// redirect to your new domain.
}

Well its a normal programming habit of mine to put the string contant
first to avoid any null pointer exceptions in java.
Second I remember seeing an exception thrown by IE browser when I did
local.search.indexOf saying "for security this operation not allowed"
or something similar.
I cannot guarantee that I can reproduce, but is there such a
restriction.
 
S

Sanjay

Sanjay wrote:
Well its a normal programming habit of mine to put the string contant
first to avoid any null pointer exceptions in java.
Second I remember seeing an exception thrown by IE browser when I did
local.search.indexOf saying "for security this operation not allowed"

Oops its location.search.
 
T

Thomas 'PointedEars' Lahn

Sanjay said:
Well its a normal programming habit of mine to put the string contant
first to avoid any null pointer exceptions in java.

You have not understood that the order in which you use the parameters
affects the semantics of the expression.

"MY_OLD_DOMAIN".indexOf(location.host) != -1

could evaluate not only to

"MY_OLD_DOMAIN".indexOf("MY_OLD_DOMAIN") != -1

which would evaluate to `true', but also to

"MY_OLD_DOMAIN".indexOf("foo.MY_OLD_DOMAIN") != -1

which will _never_ evaluate to `true' even if this is the same second-level
domain. But if you want only equality to evaluate to `true', as in

if ("MY_OLD_DOMAIN".indexOf(location.host) != -1)

then there is no need for String.prototype.indexOf() anyway:

if (location.host == "MY_OLD_DOMAIN")

Second, Java != JavaScript. There are no NullPointerExceptions in
JS/ECMAScript, there are no pointers at all.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Sanjay said:
Ha ha a good one.

If you do not have something intelligent to say, you should just shut up.
Unless you deliberately try to make a fool of yourself, of course.


PointedEars
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top