1

I have this jquery which runs and reloads all pages on my site at the moment. Problem is that I would prefer if the script does not reload certain pages.

For example: if urls contains "string-1" and/or "string-2" don't reload page. (where the strings are folders /blog/ for example). Is that possible? Thanks

$(function(){
    $('body').fadeIn(1000);
    if (localStorage.reloaded !== 'true') {
        setTimeout(function(){
            $('body').fadeOut(2000, function(){
                localStorage.reloaded = 'true';
                location.reload(true);
            });
        }, 5000); // 5 seconds for demo
    }
});

3 Answers 3

1

You can check for the existence of one string within another by using indexOf(). Try this:

var loc = window.location.href;
if (localStorage.reloaded !== 'true' && (loc.indexOf('string-1') != -1 || loc.indexOf('string-2') != -1)) {
    setTimeout(function(){
        $('body').fadeOut(2000, function(){
            localStorage.reloaded = 'true';
            location.reload(true);
        });
    }, 5000); // 5 seconds for demo
}
Sign up to request clarification or add additional context in comments.

Comments

0

try this......

$(function(){

if(window.location.href.indexOf("string-1")!=-1)
    {
$('body').fadeIn(1000);
    if (localStorage.reloaded !== 'true') {
        setTimeout(function(){
            $('body').fadeOut(2000, function(){
                localStorage.reloaded = 'true';
                location.reload(true);
            });
        }, 5000); // 5 seconds for demo
    }

}
});

Comments

0
$(document).ready(function() {
  if (window.location.href.indexOf("string-1") > -1 || window.location.href.indexOf("string-2") > -1) {
    $('body').fadeIn(1000);
    if (localStorage.reloaded !== 'true') {
      setTimeout(function() {
        $('body').fadeOut(2000, function() {
          localStorage.reloaded = 'true';
          location.reload(true);
        });
      }, 5000); // 5 seconds for demo
    }
  }
});

This should work

3 Comments

Thanks! if I want to add also a page, will I just need to replace the "string-1" with "examlple.com/page" ?
i would only use the page name after "example.com"... everything that comes after the "/", this should work without any problem :)
Great thanks. But it doesn't work in firefox, however in Chrome is all fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.