[英]Match string with clicked URL on unload of current page
我有一个带有URL的表格:
localhost:83/mysite/item/UpdateItems/225
我有一个临时表,已在其中装载数据并从临时表以表格形式显示。
当我刷新页面时,我不想触发ajax调用并截断表。 但是,如果我单击菜单中的另一个链接,那么我确实想触发ajax调用并截断表。
可能吗?
单击另一个菜单时,我已使用beforeunload
截断表:
$(window).on('beforeunload', function(){
//My ajax code for truncate table
});
但这也可以在页面刷新上使用,我不需要。 所以我尝试在URL中匹配字符串:
$(window).on('beforeunload', function(e){
if (document.URL.indexOf("UpdateItems") > -1) {
console.log("page refresh");
}else{
console.log("not refreshed");
}
});
但是,即使我单击了另一个菜单,也始终处于首页刷新状态。
试试这个,肯定会起作用,
<SCRIPT language="javascript">
window.onbeforeunload=before;
function before(evt)
{
if(window.location.toString().indexOf("UpdateItems") > -1) {
console.log("page refresh");
}
else{
console.log('Not');
}
}
</script>
如果刷新是通过单击页面上的链接(而不是从脚本说或用户单击浏览器的刷新按钮)完成的(如果这是一个很大的数目),则此方法可能有效:
每当有人单击不应导致表被截断的链接时,请将标志设置为false,并且仅在标志为true时才运行代码:
// As default the flag is true.
flagTruncate = true;
// When someone clicks a link with UpdateItems in the URL, the flag is set to false.
$("a[href*='UpdateItems']").click(function () {
flagTruncate = false;
});
//Only truncate the table when the user is leaving if the flag is still true.
$(window).on('beforeunload', function() {
if(flagTruncate) {
//Truncate the table.
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.