volume one
volume one

Reputation: 7563

Check for window.location.href.indexOf doesn't work

I'm trying to call a page on my server every second, only if the current page the user is on is a Create page or an Edit page. But this code does not work:

function sessionRefresh(){
var remoteURL = '/path/to/file';
    $.get(remoteURL,  function(data){
      setTimeout('sessionRefresh()',1000);
    });
}

if(window.location.href.indexOf("/create" != -1) || window.location.href.indexOf("/edit" != -1)) {
    setTimeout('sessionRefresh()',1000);
}

The sessionRefresh() function is running on all pages. But I only want it to run if the URL has /create or /edit in it. Just to note, I'm not going to get this file every second on the production server. Its just for testing.

How can I rewrite this so that the function is only called on those create and edit pages?

Upvotes: 0

Views: 1472

Answers (2)

Rajashekhar Rangappa
Rajashekhar Rangappa

Reputation: 526

Replace

if(window.location.href.indexOf("/create" != -1) || window.location.href.indexOf("/edit" != -1)) {
setTimeout('sessionRefresh()',1000);

}

with

if(window.location.href.indexOf("/create") != -1 || window.location.href.indexOf("/edit") != -1) { setTimeout('sessionRefresh()',1000); }

Upvotes: -1

Serge K.
Serge K.

Reputation: 5323

You made a copy-paste typo, try

if(window.location.href.indexOf("/create") != -1 || window.location.href.indexOf("/edit") != -1) {

instead of

if(window.location.href.indexOf("/create" != -1) || window.location.href.indexOf("/edit" != -1)) {

Upvotes: 2

Related Questions