通常情況下,我們可以使用防止F5刷新頁面與javascript
如下如何防止F5當光標在URL
if (keyCode == 116)
event.preventDefault();
但我想,以防止F5刷新,從工作時,光標在URL。這不是應用程序刷新,而是刷新頁內瀏覽器。 當光標位於URL上並且用戶按下F5鍵時,頁面將刷新。我需要防止這一點。
請幫幫我。
通常情況下,我們可以使用防止F5刷新頁面與javascript
如下如何防止F5當光標在URL
if (keyCode == 116)
event.preventDefault();
但我想,以防止F5刷新,從工作時,光標在URL。這不是應用程序刷新,而是刷新頁內瀏覽器。 當光標位於URL上並且用戶按下F5鍵時,頁面將刷新。我需要防止這一點。
請幫幫我。
你不能防止這種情況發生。一旦頁面上的焦點丟失(即當您點擊URL欄時),javascript將不再接收鍵盤事件。
但是,您可以向用戶顯示一條消息,以警告他們即將離開該頁面。
window.onbeforeunload = function(){
return "Your message here";
};
,當他們試圖離開頁面(或刷新它),可以選擇離開或留在網頁上的用戶將被提示消息。你不能完全阻止用戶重新加載,但如果他們這樣做,你可以讓它聽起來真的很可怕。
,如果你的光標在位置不能檢查,如果鼠標光標如果在URL,但你可以檢查[0,0]
var cursorX;
var cursorY;
document.onmousemove = function(e){
cursorX = e.pageX;
cursorY = e.pageY;
}
$(document).on("keydown", function(e){
if ((e.which || e.keyCode) == 116 && (cursorX == 0 && cursorY == 0))
e.preventDefault();
});
如果cursorX和粗略等於0,則防止您的行動
您可以使用的onmouseover跟蹤,當鼠標移動到URL,然後你可以使用的onmouseout跟蹤時它是關閉的URL。看下面的例子。
<a onmouseover="overIsTrue()"
onmouseout="overIsFalse() href="http://www.yourpage.com">here</a>
//keep track of the mouse state
var overURL = false;
window.addEventListener('keydown', function (event) {
// if the key is 116 and the mouseover is true
if (event.keyCode === 116 && overURL) {
// prevent default behaviour
event.preventDefault();
return false;
}
});
//mouse over is true
function overIsTrue() {
overURL = true;
}
//mouse over is false
function overIsFalse() {
overURL = false;
}
你,我們可以得到的位置,但是,如何阻止瀏覽器刷新 – Srikanth
@Srikanth編輯我的回答 – Gwendal
你的代碼執行時,光標在我們的應用程序,但我會繼續URL鼠標光標,點擊F5然後將頁刷新,其瀏覽器功能。我希望你得到了我要告訴的 – Srikanth