防止页面导航

时间:2017-05-12 21:45:34

标签: javascript jquery

我正在努力发现页面上所有可能的功能,所以使用它来模拟所有元素的点击/鼠标悬停:

$( document ).ready(function() {
 var elements = $("body").find("*");
 setTimeout(function () { 
    elements.each(function(e){
       elements[e].click();
 }, 400)
});   

如果我这样做,我可以让它工作,但显然会遗漏很多物品。

$( document ).ready(function() {
var elements = $("body").find("*");


setTimeout(function () { 
    elements.each(function(e){
        if (elements[e].localName == "a" | elements[e].localName == "span"  | elements[e].localName == "li" | elements[e].localName == "i"){
                console.log(elements[e]);
        }
        if (elements[e].localName != "a" && elements[e].localName != "span"  && elements[e].localName != "li" && elements[e].localName != "i"){
            if (!elements[e].href){


                elements[e].click();
            }
        }
    })
}, 400)

});   

我尝试过这样的功能:

window.onbeforeunload = function() {
    return 'false';
}
// or this
window.addEventListener("beforeunload", function (e) {
    event.preventDefault();
});

但是这些被firefox忽略了,我最终在登录页面上,或者尝试提交表单或导航中的其他链接。我不能只从搜索中排除所有元素,我该怎么办?

1 个答案:

答案 0 :(得分:0)

必须是:

function move(moveName, gameID, playerID){
    var moveNameID = moveName.replace(/\s+/g, "").toLocaleLowerCase();
    var postParams = { 'guid' : gameID,
                       'pid' : playerID,
                       'movename' : moveNameID};
    var ajaxPromise = new AjaxPostPromise("url", postParams);
    //show your loading gif as you make your ajax call
    document.getElementById("loading").classList.remove("hidden");
    ajaxPromise
        .then( function(response) {
            // hide the gif after the promise is returned, whether successful or not
            document.getElementById("loading").classList.add("hidden");
            alert(response)
         })
        .catch( function(errorMessage){
            // here too (in case the request failed)
            document.getElementById("loading").classList.add("hidden");
            alert(errorMessage);
        });
}