merxie
merxie

Reputation: 95

If URL contains string don't run jQuery function?

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
    }
});

Upvotes: 1

Views: 521

Answers (3)

Marcel Wasilewski
Marcel Wasilewski

Reputation: 2679

$(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

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

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
}

Upvotes: 1

amrit sandhu
amrit sandhu

Reputation: 130

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
    }

}
});

Upvotes: 0

Related Questions