什么元素触发页面退出

发布于 2024-12-29 14:22:02 字数 344 浏览 3 评论 0原文

我试图找到触发页面退出的元素。假设他们点击了 a href...我将把 JS 发起的位置更改留给另一场战斗。我想在退出之前做一些处理(即保存应用程序的状态)。

我尝试过的方法:

$(window).bind("unload", function(e) { console.log("====EXIT===="); console.log($(this)); console.dir(e); } );

$(this) 和“e”(事件)都没有引用导致它的元素。

  • $(this) 什么也没有
  • ,“e”打印一个空对象“c.Event”

I'm trying to find the element that triggered a page exit. Say they click on an a href... I'll leave the JS-initiated location changes for another battle. I want to do some processing before exit (ie, save state of app).

What I've tried:

$(window).bind("unload", function(e) { console.log("====EXIT===="); console.log($(this)); console.dir(e); } );

Neither the $(this) nor "e" (event) reference the element that caused it.

  • $(this) is nothing
  • and "e" prints an empty object "c.Event"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱武穆 2025-01-05 14:22:02

我强调这个对你有用。

$(document).ready(function(){

    $('a').click(function(event){
        var pageUrl = $(this).attr("href");
        if(pageUrl.indexOf('google.com') == -1 && !$(this).attr("rel") && pageUrl.indexOf('javascript')==-1){
            //only your domain, lightboxes, and javascripts, otherwise cancel event.
            event.preventDefault(); 
            console.log('sorry dude no another site');
        }
    });

});

尝试这些;

<a href="http://google.com">google</a>
<a href="http://yahoo.com">another site</a>
<a href="javascript:alert(1)">alert</a>
<a href="http://www.gravatar.com/avatar/b46c1b6ff31e665600a62482f197622f?s=32&d=identicon&r=PG" rel="ligthbox">image</a>

i pressume this one will work for you.

$(document).ready(function(){

    $('a').click(function(event){
        var pageUrl = $(this).attr("href");
        if(pageUrl.indexOf('google.com') == -1 && !$(this).attr("rel") && pageUrl.indexOf('javascript')==-1){
            //only your domain, lightboxes, and javascripts, otherwise cancel event.
            event.preventDefault(); 
            console.log('sorry dude no another site');
        }
    });

});

Try with theese;

<a href="http://google.com">google</a>
<a href="http://yahoo.com">another site</a>
<a href="javascript:alert(1)">alert</a>
<a href="http://www.gravatar.com/avatar/b46c1b6ff31e665600a62482f197622f?s=32&d=identicon&r=PG" rel="ligthbox">image</a>
拿命拼未来 2025-01-05 14:22:02

JavaScript 事件是“onBeforeUnload”,其对应的 jQuery 事件是“beforeunload”:

$(window).bind('beforeunload', function() {
  confirm("You know you're leaving this page, right?");
});

编辑 -

确定哪个元素导致 URL 更改的唯一方法是挂钩页面上的所有元素。例如:

var lastElementClicked;

$('a').click(function(){
  lastElementClicked = this;
});

然后您可以使用 lastElementClicked 对象的属性 - 例如 hrefdata()html() - 在 beforeUnload 中执行逻辑。

The JavaScript event is "onBeforeUnload" and its corresponding jQuery event is 'beforeunload':

$(window).bind('beforeunload', function() {
  confirm("You know you're leaving this page, right?");
});

EDIT -

The only way to determine which element caused a URL change is to hook all of the elements on the page. For example:

var lastElementClicked;

$('a').click(function(){
  lastElementClicked = this;
});

You can then use the lastElementClicked object's attributes -- such as href, data() or html() -- in the beforeUnload to do your logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文