Why can't I expand this event object in the chrome console?

余生颓废 提交于 2019-12-05 01:48:15
Paul S.

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;
}

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.

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.

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.

Old question. But this simple solution worked better for me. Use

function(e) {
  console.dir(e);
}

It's a good practice when inspecting objects or arrays that can change later to drop in a

debugger

...after the point in the code you want to debug. This pauses the execution of the code at this point and means you know for sure you're seeing its state at this point, rather than any subsequent changes.

The failure to expand can mean that the object was subsequently deleted or garbage-collected, or (especially if you're using Chrome dev tools on a Node.js script) that the script is complete, so all refs point nowhere.


In fact, an arguably better practice is to use debugger instead of console.log, then find what you want to inspect in the local variables.

window.onbeforeunload = function (e) {
  debugger; // Now go find `e` in the local variables section
}

I just came across this question with a problem I had where my API PUT request was showing as cancelled in my console tab in chrome dev tools and I was seeing the same behavior where I couldn't expand the object and the little i icon was showing next to the console entry. I decided to post this answer with a link to my question in case it might help anyone else.

Api PUT request showing as "cancelled" with error message "TypeError: failed to fetch"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!