Forum Moderators: open
mypath="http://www.example.com/somepage.html";
if ( top.location.href.substr(0,mypath.length) != mypath ) eval( "top." + "location." + "href" + "= '" + mypath + "';" );
/**
* @param u {Object} The configuration object which consists of valid hostnames
* as "keys" and the url to redirect to when a bad site is found.
*/
(function (u) {
var w = this, // the window object
t = w.top, // the top property of the window object
l = t.location; // the location property of the top window
if (!u.hosts[l.hostname]) { // Check to see if this host is in the hosts list
// If not, replace (not redirect) to the default location for this page
// and include the bad site where it might be logged for tracking purposes
l.replace(u.default + "?badsite=" + encodeURIComponent(l.href));
}
})({
hosts: {
"www.example.com": 1,
"www.example.net": 1
},
default: "http://www.example.net/mypage.htm"
});
(function(u){var w=this,t=w.top,l=t.location;if(!u.hosts[l.hostname]){l.replace(u.default+"?badsite="+encodeURIComponent(l.href));}})(
{hosts:{"www.example.com":1,"www.example.net":1},default:"http://www.example.net/mypage.htm"});
<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="http://www.example.com/mypage.htm" >
<title>My Page</title>
</head>
<body>
<h1>My Page</h1>
<script>
function getCanonical() {
var i,
lnks = document.getElementsByTagName('link');
for (i = 0; i < lnks.length; i++) {
if (lnks[i].rel && lnks[i].rel.toLowerCase() == 'canonical') {
return lnks[i].href;
break;
}
}
return null;
}
/**
* @param u {Object} The configuration object which consists of valid hostnames
* as "keys" and the url to redirect to when a bad site is found.
*/
(function (u) {
var w = this, // the window object
t = w.top, // the top property of the window object
l = t.location; // the location property of the top window
if (!u.hosts[l.hostname]) { // Check to see if this host is in the hosts list
// If not, replace (not redirect) to the default location for this page
// and include the bad site where it might be logged for tracking purposes
l.replace(u.default + "?badsite=" + encodeURIComponent(l.href));
}
})({
hosts: {
"www.example.com": 1,
"www.example.net": 1
},
default: getCanonical() || "http://www.example.com/mypage.htm"
});
</script>
</body>
</html>