Вопрос: Как обновить страницу с помощью jQuery?


Как обновить страницу с помощью jQuery?


2017


источник


Ответы:


использование location.reload():

$('#something').click(function() {
    location.reload();
});

reload()функция принимает необязательный параметр, который может быть установлен на trueдля принудительной перезагрузки с сервера, а не с кэша. Параметр по умолчанию false, поэтому по умолчанию страница может перезагрузиться из кеша браузера.


3270



Это должно работать на всех браузерах даже без jQuery:

location.reload();

402



Есть множественный неограниченные способы обновления страницы с помощью JavaScript:

  1. location.reload()
  2. history.go(0)
  3. location.href = location.href
  4. location.href = location.pathname
  5. location.replace(location.pathname)
  6. location.reload(false)

    Если нам нужно было вытащить документ из    веб-сервер снова (например, где содержимое документа    динамически меняются), мы передадим аргумент как true,

Вы можете продолжить создание списка:

var methods = [
  "location.reload()",
  "history.go(0)",
  "location.href = location.href",
  "location.href = location.pathname",
  "location.replace(location.pathname)",
  "location.reload(false)"
];

var $body = $("body");
for (var i = 0; i < methods.length; ++i) {
  (function(cMethod) {
    $body.append($("<button>", {
      text: cMethod
    }).on("click", function() {
      eval(cMethod); // don't blame me for using eval
    }));
  })(methods[i]);
}
button {
  background: #2ecc71;
  border: 0;
  color: white;
  font-weight: bold;
  font-family: "Monaco", monospace;
  padding: 10px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.5s ease;
  margin: 2px;
}
button:hover {
  background: #27ae60;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


328



Много способов будет работать, я полагаю:

  • window.location.reload();
  • history.go(0);
  • window.location.href=window.location.href;

180



Чтобы перезагрузить страницу с помощью jQuery, выполните следующие действия:

$.ajax({
    url: "",
    context: document.body,
    success: function(s,x){
        $(this).html(s);
    }
});

Подход, который я использовал, был Ajax jQuery. Я тестировал его на Хром 13. Затем я помещаю код в обработчик, который вызовет перезагрузку. URL является "", что значит эта страница ,


112



If the current page was loaded by a POST request, you may want to use

window.location = window.location.pathname;

instead of

window.location.reload();

because window.location.reload() will prompt for confirmation if called on a page that was loaded by a POST request.


96



The question should be,

How to refresh a page with JavaScript

window.location.href = window.location.href; //This is a possibility
window.location.reload(); //Another possiblity
history.go(0); //And another

You're spoiled for choice.


54



You may want to use

location.reload(forceGet)

forceGet is a boolean and optional.

The default is false which reloads the page from the cache.

Set this parameter to true if you want to force the browser to get the page from the server to get rid of the cache as well.

Or just

location.reload()

if you want quick and easy with caching.


46



Three approaches with different cache-related behaviours:

  • location.reload(true)

    In browsers that implement the forcedReload parameter of location.reload(), reloads by fetching a fresh copy of the page and all of its resources (scripts, stylesheets, images, etc.). Will not serve any resources from the cache - gets fresh copies from the server without sending any if-modified-since or if-none-match headers in the request.

    Equivalent to the user doing a "hard reload" in browsers where that's possible.

    Note that passing true to location.reload() is supported in Firefox (see MDN) and Internet Explorer (see MSDN) but is not supported universally and is not part of the W3 HTML 5 spec, nor the W3 draft HTML 5.1 spec, nor the WHATWG HTML Living Standard.

    In unsupporting browsers, like Google Chrome, location.reload(true) behaves the same as location.reload().

  • location.reload() or location.reload(false)

    Reloads the page, fetching a fresh, non-cached copy of the page HTML itself, and performing RFC 7234 revalidation requests for any resources (like scripts) that the browser has cached, even if they are fresh are RFC 7234 permits the browser to serve them without revalidation.

    Exactly how the browser should utilise its cache when performing a location.reload() call isn't specified or documented as far as I can tell; I determined the behaviour above by experimentation.

    This is equivalent to the user simply pressing the "refresh" button in their browser.

  • location = location (or infinitely many other possible techniques that involve assigning to location or to its properties)

    Only works if the page's URL doesn't contain a fragid/hashbang!

    Reloads the page without refetching or revalidating any fresh resources from the cache. If the page's HTML itself is fresh, this will reload the page without performing any HTTP requests at all.

    This is equivalent (from a caching perspective) to the user opening the page in a new tab.

    However, if the page's URL contains a hash, this will have no effect.

    Again, the caching behaviour here is unspecified as far as I know; I determined it by testing.

So, in summary, you want to use:

  • location = location for maximum use of the cache, as long as the page doesn't have a hash in its URL, in which case this won't work
  • location.reload(true) to fetch new copies of all resources without revalidating (although its not universally supported and will behave no differently to location.reload() in some browsers, like Chrome)
  • location.reload() to faithfully reproduce the effect of the user clicking the 'refresh' button.

26