All Topics
Business
Careers and Education
Consulting
Finance
Management
Marketing
IT
Apache Web Server
Computer Hardware
Content Management Systems
Data Management
Desktop Software
IIS / Internet Information Server
Mac OS X
Microsoft Windows / Windows 7 / Vista / XP
Mobile Phones and PDAs
Networking - Hardware / Configuration
Security
Unix / Linux / BSD
Web Applications
Windows Server 2003 / 2008
Development
.NET Framework
APIs
ASP / Active Server Pages
ASP.NET
Algorithms / Advanced Math
C / C++
C# / C Sharp
ColdFusion
DB2 Database
Flash / Actionscript
HTML / CSS
Java
JavaScript / Ajax / DHTML
Microsoft Access / VBA
Microsoft SQL Server
Mobile Development
MySQL Database
Oracle Database
PHP
Perl
PostgreSQL Database
Python
Ruby / Ruby on Rails
Software Development
Visual Basic .NET
Visual Basic 4 / 5 / 6
XAML / WPF / Silverlight
XML
sign up
login
362,715 Members 3188 Browsing Online
Community for Developers & IT Professionals
Post New Question "
Articles Questions FAQ
home > javascript / ajax / dhtml > javascript / ajax / dhtml questions
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
11 Replies
P: n/a
Sanjay
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
Thomas 'PointedEars' Lahn
Sanjay wrote:
[color=blue]
> I guess you can do this.
>
> if ("MY_OLD_DOMAIN".indexOf(location.host) != -1 )
> {
> //redirect to your new domain.
> }[/color]
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:
PointedEars
Jan 10 '06 #3
P: n/a
*********@*****.***
Thomas 'PointedEars' Lahn wrote:[color=blue]
> 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: [/color]
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
*********@*****.*** wrote:[color=blue]
> 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?[/color]
Hi there!
I just purchased a new domain, so please
update your bookmarks if needed.
If your browser will not redirect you
automatically in a few seconds, please follow
this link
:-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
Sanjay
Thomas 'PointedEars' Lahn wrote:
[color=blue]
> 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.
> }[/color]
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
[color=blue]
>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"[/color]
Oops its location.search.
Jan 11 '06 #9
P: n/a
Thomas 'PointedEars' Lahn
Sanjay wrote:
[color=blue]
> Thomas 'PointedEars' Lahn wrote:[color=green]
>> 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.
>> }[/color]
>
> Well its a normal programming habit of mine to put the string contant
> first to avoid any null pointer exceptions in java.[/color]
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
Sanjay
Ha ha a good one.
Jan 11 '06 #11
P: n/a
Thomas 'PointedEars' Lahn
Sanjay wrote:
[color=blue]
> Ha ha a good one.[/color]
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
Help answer this question
Post your Question "
Didn't find the answer to your JavaScript / Ajax / DHTML question?
You can also browse similar questions: JavaScript / Ajax / DHTML
Question stats
viewed: 3604
replies: 11
date asked: Jan 10 '06
Most Active
acoder11972
gits4201
Dormilich2357
dmjpro1113
pbmods926
Most Questions Answered
Dormilich86
gits55
acoder49
RamananKalirajan19
JKing13
Top Questions
Check file size with Javascript144209
114112
how to get selected item or value of html-combo box through javascript?113370
OnClick change element css class92854
connect to an access database using javascript78628
Latest Questions
Full screen on load102
How to Put PHP Code INSIDE JavaScript Conditional Statements?119
Imitating an image selection feature from another website181
Small fading bug155
function output as form value146
Similar topics
How to implement an Ajax script?
Script to check n number of values in n number of rows
Problem with Script
Problem with a script
How to work out which script to call to Save
add a script in global.asax
Script not working
Best Java Script Scroller
Marquee script question
Script, Save, Export SQL Database Diagrams
More
Latest Questions
Latest Expert Insights
Need Help with Bytes? (FAQ)
BYTES.COM c 2012
Formerly "TheScripts.com" from 2005-2008About Bytes Advertise on Bytes
Sitemap JavaScript / Ajax / DHTML Answers Sitemap JavaScript / Ajax / DHTML InsightsSitemap
Follow us to get the Latest Bytes Updates