English 中文(简体)
离开网页前的Javales
原标题:JavaScript before leaving the page

I want to make a confirmation before user leaving the page. If he says ok then it would redirect to new page or cancel to leave. I tried to make it with onunload

<script type="text/javascript">
function con() {
    var answer = confirm("do you want to check our other products")
    if (answer){

        alert("bye");
    }
    else{
        window.location = "http://www.example.com";
    }
}
</script>
</head>

<body onunload="con();">
<h1 style="text-align:center">main page</h1>
</body>
</html>

但它在网页已经关闭后确认? 如何适当做到这一点?

如果有人表明如何用 j子做,那会更好吗?

问题回答

<代码>onunload(或on beforeunload)不能将用户转至另一页。 这是出于安全原因。

如果在用户离开网页之前要迅速显示,请使用<代码>on beforeunload/code>:

window.onbeforeunload = function(){
  return  Are you sure you want to leave? ;
};

或与 j合:

$(window).bind( beforeunload , function(){
  return  Are you sure you want to leave? ;
});

这只是问用户是否希望离开网页,如果他们选择留在网页上的话,你就无法转头。 如果他们选择离开,浏览器将前往他们告诉他们去的地方。

您可在该网页卸载前使用onunload,但不能从该网页上调方向(onunload内的14+区段警报):

window.onunload = function() {
    alert( Bye. );
}

或与 j合:

$(window).unload(function(){
  alert( Bye. );
});

当你发现形式状态时,该守则已经改变。

$( #form ).data( serialize ,$( #form ).serialize()); // On load save form current state

$(window).bind( beforeunload , function(e){
    if($( #form ).serialize()!=$( #form ).data( serialize ))return true;
    else e=null; // i.e; if form state change show warning box, else don t show it.
});

谷歌剧本能发挥作用,它将收集所有形式投入,并在阵列中予以节省。 我认为,这一解释足以:

在我没有数据时,我确实表明这一确认信息。

window.onbeforeunload = function() {
  if (isDirty) {
    return  There is unsaved data. ;
  }
  return undefined;
}

返回<代码>un Defin后,确认无效

注:<代码>null 不与IEE合作

也可使用<代码>un specified,以代替确认

window.onbeforeunload = undefined;

这将提醒人们不要上下页。

<script type= text/javascript >
function goodbye(e) {
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue =  You sure you want to leave? ; //This is displayed on the dialog

    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}
window.onbeforeunload=goodbye; 

</script>

为了pop14+,你需要做以下工作:

jQuery(window).bind( beforeunload , function(){
    return  my text ;
});

将询问用户是否希望停留或离开。

您可在always查询用户后离开网页。

window.onbeforeunload = s => "";

请用户在网页上的内容经过修改后,见

通常,当用户以某种形式作出改变时,你想要显示这一信息,但这种信息却得不到拯救。

Take this approach to show a message, only when the user has changed something

var form = $( #your-form ),
  original = form.serialize()

form.submit(function(){
  window.onbeforeunload = null
})

window.onbeforeunload = function(){
  if (form.serialize() != original)
    return  Are you sure you want to leave? 
}

Just a bit more helpful, enable and disable

$(window).on( beforeunload.myPluginName , false); // or use function() instead of false
$(window).off( beforeunload.myPluginName );




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签