javascript - opener - js轉址
如何使用jQuery刷新頁面? (15)
如何使用jQuery刷新頁面?
jQuery Load
函數也可以執行頁面刷新:
$('body').load('views/file.html', function () {
$(this).fadeIn(5000);
});
window.location.reload()
將從服務器重新加載,並將再次加載所有數據,腳本,圖像等。
因此,如果您只想刷新HTML, window.location = document.URL
將返回更快,流量更少。 但如果URL中存在哈希(#),則不會重新加載頁面。
以下是您可以使用jQuery重新加載頁面的一些代碼行。
它使用jQuery包裝器並提取本機dom元素。
如果您只想在代碼上使用jQuery ,並且不關心代碼的速度/性能 ,請使用此選項。
只需從1到10中選擇適合您的需求,或者在此之前根據模式和答案添加更多內容。
<script>
$(location)[0].reload(); //1
$(location).get(0).reload(); //2
$(window)[0].location.reload(); //3
$(window).get(0).location.reload(); //4
$(window)[0].$(location)[0].reload(); //5
$(window).get(0).$(location)[0].reload(); //6
$(window)[0].$(location).get(0).reload(); //7
$(window).get(0).$(location).get(0).reload(); //8
$(location)[0].href = ''; //9
$(location).get(0).href = ''; //10
//... and many other more just follow the pattern.
</script>
你不需要 jQuery中的任何東西,使用純JavaScript重新加載頁面,只需在location屬性上使用reload函數,如下所示:
window.location.reload();
默認情況下,這將使用瀏覽器緩存(如果存在)重新加載頁面...
如果您想強制重新加載頁面,只需將真值傳遞給重載方法,如下所示......
window.location.reload(true);
此外,如果您已經在窗口範圍內 ,您可以擺脫窗口並執行:
location.reload();
使用onclick="return location.reload();"
在按鈕標籤內。
<button id="refersh-page" name="refersh-page" type="button" onclick="return location.reload();">Refesh Page</button>
使用JavaScript刷新頁面有很多方法:
-
location.reload()
-
history.go(0)
-
location.href = location.href
-
location.href = location.pathname
-
location.replace(location.pathname)
location.reload(false)
如果我們需要再次從Web服務器中提取文檔(例如文檔內容動態更改),我們會將參數傳遞為
true
。
您可以繼續創作列表:
-
window.location = window.location
-
window.self.window.self.window.window.location = window.location
- ......以及其他534種方式
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>
即使沒有jQuery,這應該適用於所有瀏覽器:
location.reload();
問題應該是,
如何使用JavaScript刷新頁面
window.location.href = window.location.href; //This is a possibility
window.location.reload(); //Another possiblity
history.go(0); //And another
你被寵壞了選擇。
如果當前頁面是由POST請求加載的,您可能想要使用
window.location = window.location.pathname;
代替
window.location.reload();
因為如果在POST請求加載的頁面上調用, window.location.reload()
將提示確認。
它是JavaScript中最短的。
location = '';
我想有很多方法可行:
-
window.location.reload();
-
history.go(0);
-
window.location.href=window.location.href;
我發現
window.location.href = "";
要么
window.location.href = null;
也刷頁面。
這使得重新加載刪除任何哈希的頁面變得非常容易。 當我在iOS模擬器中使用AngularJS時,這非常好,所以我不必重新運行應用程序。
這是一個使用jQuery異步重新加載頁面的解決方案。 它避免了window.location = window.location
引起的閃爍。 此示例顯示了一個連續重新加載的頁面,如儀表板中所示。 它經過了久經考驗,並在時代廣場的信息顯示電視上運行。
<!DOCTYPE html>
<html lang="en">
<head>
...
<meta http-equiv="refresh" content="300">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script>
function refresh() {
$.ajax({
url: "",
dataType: "text",
success: function(html) {
$('#fu').replaceWith($.parseHTML(html));
setTimeout(refresh,2000);
}
});
}
refresh();
</script>
</head>
<body>
<div id="fu">
...
</div>
</body>
</html>
筆記:
- 使用
$.ajax
直接像$.get('',function(data){$(document.body).html(data)})
會導致css / js文件被緩存破壞 ,即使你使用cache: true
,這就是我們使用parseHTML
的原因 -
parseHTML
不會找到一個body
標籤,所以你的整個身體需要進入一個額外的div,我希望這一天的知識可以幫助你,你可以猜到我們如何為這個div
選擇id - 使用
http-equiv="refresh"
以防萬一javascript / server hiccup出現問題,那麼頁面將無需重新加載而無需撥打電話 - 這種方法可能以某種方式洩漏內存,
http-equiv
刷新修復了這一點
$('#something').click(function() {
location.reload();
});
reload()
函數接受一個可選參數,該參數可以設置為true
以強制從服務器而不是緩存重新加載。 該參數默認為false
,因此默認情況下,頁面可能會從瀏覽器的緩存中重新加載。