Redirect By URL Name

[Total: 0    Average: 0/5]

This example shows how to use javascript code to check the current URL to determine if that URL should map to another URL.

Head Code:

<script>
// This array contains the possible URLs that need to be redirected
var _a = new Array();
    _a[_a.length?_a.length:0] = "www.jsexamples.com";
    _a[_a.length?_a.length:0] = "www.js-examples.com";
// This is the destination URL
// These must be in the same exact order as the _a vars so that// they match the URL entered by the user and the target URL
var _t = new Array();
    _t[_t.length?_t.length:0] = "http://www.js-examples.com/";
    _t[_t.length?_t.length:0] = "http://www.js-examples.com/js/";
// This just holds the string value of the current URL.
var _loc = document.location.toString();
// This loops through all possible URLs that need to be redirected
// if it finds one (indexOf is not -1) then send this page to// the "_target" URL.
for (var i in _a) {
  if (_loc.indexOf(_a[i]) != -1) {
    document.location = _t[i];
  }
}
</script>

Body Code:


Times Viewed: 2

Leave a Reply

Your email address will not be published. Required fields are marked *