12

Few months ago I posted this answer about how to refresh the page via JavaScript.

I provided a JSFIDDLE DEMO too:

var solutions = [
    function () { location.reload(); },
    function () { history.go(0); },
    function () { location.href = location.href; },
    function () { location.href = location.pathname; },
    function () { location.replace(location.pathname); },
    function () { location.reload(false); },
];

$("[data-func]").on("click", function () {
    solutions[parseInt($(this).attr("data-func"))]();
});

Someone noticed that location.reload() is slower than the other methos. Now I can see the same thing.

Why is it slower? Why the others are faster?

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 3
    "window.location.reload() reloads the current page with POST data, while window.location.href=window.location.href does not include the POST data." This is probably where the performance difference arise from - http://stackoverflow.com/questions/2405117/difference-between-window-location-href-window-location-href-and-window-location – Mark Walters Dec 31 '13 at 11:48
  • @MarkWalters That may be an explanation, but where is this specified in the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Location.reload)? – Ionică Bizău Dec 31 '13 at 12:39
  • http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#dom-location-reload – Mark Walters Dec 31 '13 at 13:14
  • @MarkWalters - Yes Even i dont see where its mentioned in Documentation ???? – BetaCoder Dec 31 '13 at 13:45
  • @IonicăBizău Did my answer help at all? – Wildcard27 Jan 06 '16 at 06:38
  • @matt1985 Thanks! Not a fan of W3Schools, tho, but thanks for reminding me. I probably missed to accept your answer. – Ionică Bizău Jan 06 '16 at 08:23

2 Answers2

5

Been looking for this myself and the best reference I could find is actually on w3schools.com

http://www.w3schools.com/jsref/met_loc_reload.asp

location.reload(forceGet)

forceGet:

false - Default. Reloads the current page from the cache.

true - The current page must be reloaded from the server

Community
  • 1
  • 1
Wildcard27
  • 1,437
  • 18
  • 48
  • 2
    Please do not use w3schools, as they are not canon. Here's a more-authoritative link: https://developer.mozilla.org/en-US/docs/Web/API/Location/reload – redolent Jan 03 '16 at 02:23
  • 2
    At the time I was looking for something a little more solid than w3schools but they seemed to have the simplest version of the same answer. – Wildcard27 Jan 06 '16 at 06:37
1

From the Mozilla Developement Network I guess the .reload method may fetch all files from the Server again. This would be similar to a CTRL + F5 reload.

The location.href for example, simply follows the link which may be cached. As for the MDN definition the behave is not clearly defined so I guess its browser and case specific behave.

Sully
  • 14,672
  • 5
  • 54
  • 79
Panade
  • 309
  • 3
  • 12