how to prevent address bar and enter refresh action

hi hi 40 Reputation points
2023-05-16T07:31:02.24+00:00

i use window.open in msedge to open a new page,when cursor on address bar and press enter key the page will refresh. i want to prevent this action ,i have used EventListener keydown and event.preventDefault in beforeunload but they all dont work,is there any way to prevent refresh?

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,354 questions
{count} votes

Accepted answer
  1. ShiJieLi-MSFT 10,186 Reputation points Microsoft Vendor
    2023-05-19T06:56:35.5966667+00:00

    Hi @hi hi,

    Unfortunately, you cannot disable refresh because:

    1. Keydown event (or any event) does not apply to the browser UI, which means if the focus is on the address bar, the keydown event would not be listened for. A user is able to hit ENTER key in the address bar.
    2. Either a prompt or refresh is expected in beforeunload event. And the only way to prevent refresh is a user selecting "Cancel". It always involves user interaction.

    It is a by-design behavior in modern browsers. For more information, you can refer to this doc.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Shijie Li

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.