473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

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

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
11 5401
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
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
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
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
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
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

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
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
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
Ha ha a good one.

Jan 11 '06 #11
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Clay Beatty | last post by:
When you create database diagrams in Enterprise Manager, the details for constructing those diagrams is saved into the dtproperties table. This table includes an image field which contains most of...
2
by: Jo_Calico | last post by:
I love the Dynamic Drive cross browser marquee script. I'd like to make the text loop immediately after completion, so the beginning runs right after the end (does that make sense?). Could anyone...
3
by: Razvan | last post by:
Hello, Can somebody recommend me a Java Script scroller that can scroll an i-frame ? I tried the Tigra scroller (www.softcomplex.com/products/tigra_scroller/) but sometimes it does not...
1
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. ...
3
by: caldera | last post by:
hi, I want to add a script(window.open script actually) in global.asax, for example Application_BeginRequest. I used Response.Write but opened page locked however main page doesn't. Main page...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
9
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
3
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
14
by: ankitmathur | last post by:
Hi, I'm facing a pretty peculiar problem. In one of my pages I print the number of rows depending upon the number of Qty I got filled in my previous page i.e. If Qty is filled in as 3 in...
3
by: BobBlock | last post by:
This is a script that, when a link is clicked, will pull a page from somewhere and insert it in a div in the current page. Pretty simple, yes, but being the thick head I seem to be, I can't figure...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.