0

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();
});
}
Daniel
  • 25
  • 5
  • Won't your page just refresh infinitely unless you remove the hash on reload? – timothyclifford Mar 08 '16 at 17:33
  • You can use `setTimeout(function(){ //code here }, 2000)` – CMedina Mar 08 '16 at 17:34
  • If it doesn't have to be exactly the same url, you could add a query parameter, and only refresh if it isn't there. So instead of `location.reload();` write `location.href = location.href + "?foo=bar"`. – Andrew Mar 08 '16 at 17:34
  • Possible duplicate of [One time page refresh after first page load](http://stackoverflow.com/questions/6985507/one-time-page-refresh-after-first-page-load) – Mark Schultheiss Mar 08 '16 at 17:42

3 Answers3

1
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

ameenulla0007
  • 2,663
  • 1
  • 12
  • 15
0

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();
}
radbyx
  • 9,352
  • 21
  • 84
  • 127
  • Hi, thanks for your suggestion. I did not express properly my question. I would like to refresh the page once. Is it possible? – Daniel Mar 08 '16 at 17:43
0

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