为什么不能在chrome控制台中扩展此事件对象? [英] Why can't I expand this event object in the chrome console?

查看:77
本文介绍了为什么不能在chrome控制台中扩展此事件对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简化,我正在做的是在控制台中运行此

Simplified, what I'm doing is running this in the console:

window.onbeforeunload = function (e) {
    console.log(e);
}

但是在控制台中,当事件触发时(通过在写SO问题的过程中尝试离开页面"),我看到的是:

But in the console, when the event fires (by trying to "leave page" in the middle of writing an SO question) what I see is this:

Event {clipboardData: undefined, cancelBubble: false, returnValue: true, srcElement: document, defaultPrevented: false…}

旁边有一个小"i"图形.当我单击它旁边的箭头以在控制台中展开对象时,什么也没有发生.箭头变成指示它已经扩展,但没有扩展.

With a little "i" graphic next to it. When I click the arrow next to it to expand the object in the console, nothing happens. The arrow turns to indicate that it has expanded, but it doesn't expand.

我在这里想念什么?

推荐答案

之所以会这样,是因为尽管您让控制台保留页面更改的权限,但 Object 不再存在-在以下情况下它已被销毁你离开了页面.这意味着根本无法再对其进行检查,因此单击向下的三角形无济于事.

This is happening because although you're letting the console persist over page changes, the Object no longer exists - it was destroyed when you left the page. This means it's simply not available to be inspected anymore, so clicking the down triangle is not helpful.

请尝试执行此操作,以防止页面实际更改:

Try this instead, to prevent the page actually changing:

window.onbeforeunload = function (e) {
    console.log(e);
    return true;
}

现在,页面将提示您做什么.在出现的提示中单击取消",以保留在页面上.现在,您可以根据需要在控制台中检查Event.

Now the page will prompt to ask you what to do. Click 'cancel' in the prompt that comes up in order to remain on the page. Now you can inspect the Event in the console as desired.

区别在于onbeforeunload函数现在返回的值不是null/undefined.返回值可以是任何值,甚至可以是''false等...除nullundefined之外的任何值,它仍然会导致页面在导航之前提示,从而使您有机会检查事件.请记住,在没有return语句的情况下,JavaScript函数默认情况下会返回undefined.

The difference is that the onbeforeunload function is now returning a value that isn't null/undefined. The return value could be anything, even '' or false, etc...anything except null and undefined, and it will still cause the page to prompt before navigating away and thus giving you an opportunity to inspect the event. Remember that with no return statement, JavaScript functions return undefined by default.

每当您无法在Chrome开发工具中检查某项内容时,90%的时间是因为某些操作导致该内容不可用...页面从该对象存在时就一直前进.

Whenever you can't inspect something in the Chrome Dev Tools, 90% of the time it's because some action has caused that thing to become unavailable...the page has moved on from when that object existed.

这篇关于为什么不能在chrome控制台中扩展此事件对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆