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

Redirect to server root address

P: n/a
hi all,

I am trying to re-direct a html page to the root of a server (eg.
localhost:8080/index.html). However the problem is that I dont want to
hard code the name of the server (localhost) as it will change
periodically. I want to reference just the server name (whatever it is)
and then the port no and then the file name. I hope we can achieve this
using Javascript.

So far I have written:
<META HTTP-EQUIV="REFRESH" CONTENT="3;
URL=http://localhost:8080/index.html">

can some genius help me with this.

thanks in advance.

Nov 28 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
sc*****@gmail.com said the following on 11/28/2005 1:38 AM:
hi all,

I am trying to re-direct a html page to the root of a server (eg.
localhost:8080/index.html). However the problem is that I dont want to
hard code the name of the server (localhost) as it will change
periodically. I want to reference just the server name (whatever it is)
and then the port no and then the file name. I hope we can achieve this
using Javascript.

So far I have written:
<META HTTP-EQUIV="REFRESH" CONTENT="3;
URL=http://localhost:8080/index.html">

can some genius help me with this.

thanks in advance.


var thisURL = document.location.href;
var thisDomain = document.domain;

Now, you have the URL itself, you can pick out the host, add the port,
and your file name.
You also have a reference, where supported, to the domain itself as well.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 28 '05 #2

P: n/a
Thanks for that. But I want to clarify something, will
http://javascript:this.domain:8080/index.html" work.

cheers.

Nov 28 '05 #3

P: n/a
sc*****@gmail.com said the following on 11/28/2005 1:58 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Thanks for that. But I want to clarify something, will
http://javascript:this.domain:8080/index.html" work.


It is easy enough to test but the answer is no.

You will have to build your own URL and then use JS to simply set the
document.location.href to the appropriate URL.

document.domain seems to be a property that can be set but not read.
Bummer.

Get the location.href property, then weed out the part you need. It will
be everything from the beginning up to the first '/' that is not part of
the '//'. Then, build your own URL string, set the location.href to that
string, then you are set.

Search the archives. You can probably find something already written
that will give you the current domain.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 28 '05 #4

P: n/a
I found something else within the archives. I need to do exactly this,
but am not sure how to implement it with a javascript method. could
somebody please guide me.

If this is the case, I would use Regular Expressions to return the
first match of:
/[ps]:\/\/([^\/]*\.)$\/?

Not sure on the \/... you might also try:
/[ps]://([^/]*\.)$/?

This should return the following for the following strings:
http://123.123.123.123/here.htm Returns:123.123.123.123
https://mydomain.com/here.htm Returns:mydomain.com
http://this.is.my.domain.name Returns: this.is.my.domain.name

thanks heaps.

Nov 28 '05 #5

P: n/a
sc*****@gmail.com wrote:
I found something else within the archives. I need to do exactly this,
but am not sure how to implement it with a javascript method. could
somebody please guide me.

If this is the case, I would use Regular Expressions to return the
first match of:
/[ps]:\/\/([^\/]*\.)$\/?

Not sure on the \/... you might also try:
/[ps]://([^/]*\.)$/?
Both would match `p://.'
This should return the following for the following strings:
http://123.123.123.123/here.htm Returns:123.123.123.123
https://mydomain.com/here.htm Returns:mydomain.com
http://this.is.my.domain.name Returns: this.is.my.domain.name


RFC3986, Appendix B, includes a Regular Expression for parsing URIs
that you could use.

However, you better configure your server accordingly so that it
responds with a 3xx status code, probably 301 or 302, and a
Location header that indicates the new location. Works everywhere
while your approach requires client-side script support. See also
<URL:http://www.w3.org/QA/Tips/reback>.
PointedEars
Nov 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.