Daniel
Daniel

Reputation: 25

How to refresh a page once using jQuery

I would like to ask you if it is possible to refresh a page once using jQuery.
The script below will refresh the page if the hash is equal to add.
My goal is to refresh once the related page.

 var url      = window.location.href; 
 var hash = url.substring(url.indexOf('#')+1);

 if(hash == 'add') {
 $(window).load(function(){

    location.reload();
});
}

Upvotes: 0

Views: 1142

Answers (3)

Yosvany Pedroso
Yosvany Pedroso

Reputation: 41

you can use cookie this is an example

function executeOnce () {
  var argc = arguments.length, bImplGlob = typeof arguments[argc - 1] === "string";
  if (bImplGlob) { argc++; }
  if (argc < 3) { throw new TypeError("executeOnce - not enough arguments"); }
  var fExec = arguments[0], sKey = arguments[argc - 2];
  if (typeof fExec !== "function") { throw new TypeError("executeOnce - first argument must be a function"); }
  if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { throw new TypeError("executeOnce - invalid identifier"); }
  if (decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) === "1") { return false; }
  fExec.apply(argc > 3 ? arguments[1] : null, argc > 4 ? Array.prototype.slice.call(arguments, 2, argc - 2) : []);
  document.cookie = encodeURIComponent(sKey) + "=1; expires=Fri, 31 Dec 9999 23:59:59 GMT" + (bImplGlob || !arguments[argc - 1] ? "; path=/" : "");
  return true;
}

Usage
executeOnce(callback[, thisObject[, argumentToPass1[, argumentToPass2[, …[, argumentToPassN]]]]], identifier[, onlyHere])

 function reloadPage () {
   var url      = window.location.href; 
   var hash = url.substring(url.indexOf('#')+1);

   if(hash == 'add') {
     $(window).load(function(){
      location.reload();
     });
   }
 }

executeOnce(reloadPage, null, "", "idPage");

Upvotes: 0

radbyx
radbyx

Reputation: 9650

This will reload the page.

window.location.reload();

Or do you mean?

var url = window.location.href; 
var hash = url.substring(url.indexOf('#')+1);

if(hash === 'add') {
    window.location.reload();
}

Upvotes: 0

ameenulla0007
ameenulla0007

Reputation: 2683

var url     = window.location.href; 
var hash    = url.substring(url.indexOf('#')+1);
if(hash=='add') {
    window.location.href="#added";
}

you can just use the above code, to achieve your requirement. if #add is in the url string, it reloads once with the new url, appending #added

Upvotes: 1

Related Questions