Forum Moderators: mack

Message Too Old, No Replies

Java redirect with parsing

How to redirect a web page by inserting a new switch in the URL

         

cdean6

11:48 pm on Apr 22, 2003 (gmt 0)



I need to redirect users from one 'version' of a web site to another using javascript.

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!

dmorison

4:19 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi cdean6,

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>

grahamstewart

11:26 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi cdean6,

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)

Ed_Gibbon

2:20 pm on May 8, 2003 (gmt 0)

10+ Year Member



Could someone help me with some javascript to check if the current URL has a "/" at the end of it, and if it does not, reload the page.

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