By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,083 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,083 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jan 10 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
I guess you can do this.

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

Thanks
Sanjay

Jan 10 '06 #2

P: n/a
Sanjay wrote:
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
Jan 10 '06 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
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?

Jan 10 '06 #4

P: n/a
Never mind. I added this to the script:

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

Worked like a charm. Thanks for the assist.

Jan 10 '06 #5

P: n/a
VK

tu*******@gmail.com wrote:
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

Jan 10 '06 #6

P: n/a
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!

Jan 10 '06 #7

P: n/a

Thomas 'PointedEars' Lahn wrote:
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.

Jan 11 '06 #8

P: n/a
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.

Jan 11 '06 #9

P: n/a
Sanjay wrote:
Thomas 'PointedEars' Lahn wrote:
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.


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
Jan 11 '06 #10

P: n/a
Ha ha a good one.

Jan 11 '06 #11

P: n/a
Sanjay wrote:
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
Jan 11 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.