PDA

View Full Version : Help me to make one time javascript random reload/redirect



agungbudiono
09-06-2016, 05:05 AM
I have this code :



<script type = "text/javascript">

setTimeout(function (){

function reDirect() {
x = Math.ceil(Math.random() * 86); // change 86 to 2 for your initial test
newPage = "Page" + x + ".html";
window.location = newPage;
}

reDirect();

}, 2000); // How long do you want the delay to be (in milliseconds)?

</script>


Please help me, how stop this code from continue looping (work only once)

jscheuer1
09-06-2016, 04:09 PM
That script doesn't actually loop. If you find that you are constantly executing it, it means that you are either redirecting to the same page that this script is on, or that you have this script on all possible pages that it could redirect to. One way you could probably prevent it from executing, at least immediately upon entering a new or the same page would be to add a hash to the url and test for that, skipping the code if it's present. But if the user then navigated to the next page, one with this script and using a link without the hash, it would still execute. Or, of course, you could remove the script from all but one page and make sure no one is redirected to that page. Finally one could use local storage or a cookie to set a value that, if detected, would prevent the code from executing. But people often disable those. A cookie is probably better because it can be set to expire in - say, an hour or a day, or at the end of the browser session. Local storage remains until the user clears it or rules set in the browser by the user determine it's expired. But both are unreliable, so if a user doesn't allow them, they could still get caught in this "loop".

agungbudiono
09-07-2016, 07:18 AM
You were right John, I put this script on blogspot, its mean homepage and single post is in the same page. Do you have solutions for this? Here is my sample blog : azkataqiyya.blogspot.com

agungbudiono
09-07-2016, 07:42 AM
I have another script for the same purpose :

fisrt script : not working because its continue looping
-----------


<script type="text/javascript">

var urls = new Array("/page", "/about", "/maps", "/policy");

function redirect()
{
window.location = urls[Math.floor(urls.length*Math.random())];
}

var temp = setInterval("redirect()", 2000);

</script>



second script : its working but only redirect to one url
--------------


<script type="text/javascript">
if(location.search.indexOf('r') < 0){
var hash = window.location.hash;
var loc = window.location.href.replace(hash, '');
loc += (loc.indexOf('?') < 0? '?' : '&') + 'r';
setTimeout(function(){window.location.href = loc + hash;}, 5000);
}
</script>


perhaps somebody can help me to combine this two script to create script for one time random redirect in blogspot

jscheuer1
09-07-2016, 02:04 PM
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:


<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.

agungbudiono
09-07-2016, 04:13 PM
Thank you John, you're the best

jscheuer1
09-07-2016, 09:19 PM
Great! I was wondering though - why you want to do this? I could see it as a prank, something funny, or as a way to force users who habitually view only one page to have to at least have a look at another sometimes. Either way, some users may panic or get angry, and hit the back button. If they do that, they will be redirected again. I'm not sure if that's such a great idea. We could add a cookie to prevent that in most cases. Whether you want that or not, I think I will at least give you - or others wanting this code, the option of adding that. When I have the code update I will post it here in this message.

OK, with cookie added:


<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 || document.cookie.indexOf(r + '=true') > -1){return;}
document.cookie = r + '=true; path=/';
urls = ["/page", "/about", "/maps", "/policy"];
setTimeout(function(){
window.location.href = urls[Math.floor(urls.length*Math.random())] + '?' + r;
}, 2000);
}, false);
</script>