Forum Moderators: mack
Old URL: [example.com...]
New URL: [example.com...]
Can I use javascript to read the URL the user is trying to get to and automatically redirect to the new site by inserting the new switch (in this example, /na)?
I'm told this can be done, but I'm new to Java and don't know where to start. Any tips or pointers would be very much appreciated. Thank you in advance!
The variable "window.location" contains the current page location which you access as a string using new string().
The following should do what you want...
<html>
<body>
<script type='text/javascript'>
// where we are at the moment
//
var s = new String(window.location);
// a string common to every page ending in (but not including) the first "/"
//
var m = new String('example.com');
// length of the common string used when rebuilding the new URL
//
var l = m.length;
// position of the common string in the current location
//
var p = s.indexOf(m);
// first part of the new URL - up to the end of the common string
//
var d1 = s.substring(0, p + l);
// third part of the new URL - the existing location after the common string
//
var d3 = s.substring(p + l, s.length);
// the new URL
//
var dn = d1 + '/na' + d3;
// send the browser off to the new location
//
window.location = dn;
</script>
</body>
</html>
Welcome to WebmasterWorld [webmasterworld.com]
I'm new to Java and don't know where to start
For the record, what you are using is Javascript, not Java.
"Java" is a full blown, industrial strength, object-oriented language (see [java.sun.com...] )
Whereas "Javascript" is a half-hearted browser scripting language. It has some syntax that has been lifted from Java, but the similarity ends there :)
(This might sound a little pedantic - but it will help you find the answers you are looking for)
Flowchart:
IF "/" is not the last character in the URL, i.e., if URL not equal to "http://www.mysite.com/"
THEN reload page with URL = "http://www.mysite.com/"
Thanks