That second script is an example of changing the url - it uses a query (also called a search) instead of the hash because it's reloading the same page and is intended to preserve any existing hash. Hashes most normal use are to send the user to a section on the page. But there can be only one or none hash per url, and there can be many queries or searches. Either can work if well thought out (the one in your post uses a single letter, which could be found in another query, thus short circuiting the intended initial reload), but are limited if a user navigates to another page without carrying the hash, query, or whatever it is, with them. Neither queries nor hashes were meant to be used like this though, so it's important that, before using them in this way, find out if the site you will be using them on already uses them for something else or not. If the do, it might conflict. That said, I have an idea how to work this as long as there is no conflict, and as long as there is no other navigation possible except via links. It can't be perfect because folks can use bookmarks or history and perhaps miss the initial redirect, but the very first time they get there, it should work fine.
This should go on every page:
Code:
<script type="text/javascript">
window.addEventListener('load', function(){
var r = 'redirected', hash, loc, urls, atags = document.getElementsByTagName('a'), i = atags.length, tag;
while(--i > -1){
if(!(tag = atags[i]).href || tag.search.indexOf(r) > -1){continue;}
hash = tag.hash;
loc = tag.href.replace(hash, '');
loc += (loc.indexOf('?') < 0? '?' : '&') + r + hash;
tag.href = loc;
}
if(location.search.indexOf(r) > -1){return;}
urls = ["/page", "/about", "/maps", "/policy"];
setTimeout(function(){
window.location.href = urls[Math.floor(urls.length*Math.random())] + '?' + r;
}, 2000);
}, false);
</script>
NOTE: This assumes that the addresses in the urls array have no hash or search included in them (your example ones do not), if that changes, let me know.
Bookmarks