Is js monitoring refreshed?
('beforeunload', function (event) { // Here you can perform the actions you want, such as asking the user whether to confirm leaving the page = 'https://mp./DIET/TR/US/LP2/'; })
existbeforeunload
Used in event handlersRedirecting to another page is invalid because
beforeunload
During the event, the browser blocks the page's navigation operations to ensure that the user has the opportunity to confirm whether to leave the page.
beforeunload
The event is triggered before the page is uninstalled. It is mainly used to ask the user whether to confirm leaving the page or perform some urgent cleaning operations. However, ifbeforeunload
Page redirection is attempted during the event, and the browser will block this navigation operation because the user has not yet decided whether to leave the page.
When the user clicks the refresh button, JavaScript cannot directly refresh the current page and then jump to another page, because the browser will not continue to execute other JavaScript code when refreshing. Refresh behavior resets the page status and interrupts all JavaScript execution on the current page.
However, you can do this by using Session Storage or Local Storage to jump to another page after refresh. This way, you can store the information in local storage before refreshing, and then check if the information exists when the page loads, and if so, perform a page redirect.
highlighter- javascript
// Add event listener('beforeunload', function (event) { ("Do what you want here, such as storing information to local storage") ('refreshed', 'true'); }); // Check the information in the local storage when the page is loading('load', function () { const refreshed = ('refreshed'); ("If the information in the local storage exists, page redirection will be performed") if (refreshed === 'true') { // Clear information in local storage to prevent jumping from triggering again ('refreshed'); // Execute the page to jump to another page = 'https://。。'; } });
The above code generates a bug, which means I refreshed and jumped to it, and then I want to jump back to this file from click, but the file has entered, and it will be refreshed back. Change the bug while ensuring the function.
When usingbeforeunload
When an event is happening, the event will be triggered whether it is refreshing the page or navigating to another page. This isbeforeunload
The feature of the event, which triggers before the user leaves the page, whether it is closing the page, refreshing the page, or navigating to another page.
go at the beginning is false, which means that this situation is refresh, refreshing and entering another page. Go is true, which means that it is jumping. After jumping in, you cannot execute the load method. At the same time, go returns to false
On the page
highlighter- ini
= 'https://mp./DIET/TR/US/LP2/asset/?Param=' + param+"&go=false";
In the refreshed page
highlighter- xml
<script> ('load', function () { // Check the go value in the URL parameter. If it is true, then the page will be jumped. const urlParams = new URLSearchParams(); const go = ('go'); if (go === 'true') { // Execute the page to jump to another page = 'https://。。。。'; } // Set go back to false to jump again const newUrlParams = new URLSearchParams(); ('go', 'true'); const newUrl = + '?' + (); ({}, '', newUrl); }); </script>
const newUrlParams = new URLSearchParams();
: This line creates a new URLSearchParams object to get the parameter part in the current page URL (i.e.?
the latter part).
('go', 'true');
: This line will be namedgo
The URL parameter is set totrue
. This adds a URL parametergo=true
parameters.
();
: This line converts the new URL parameter object into a string and is with the path to the current page ()and
?
Splice it to get the new URL string.
({}, '', newUrl);
: This line usesMethod to replace the URL of the current page. The first parameter
{}
is a state object, which can be an empty object. The second parameter''
It is a new URL, that is, the replaced URL, here it is built using the previous onenewUrl
variable.
This is the article about whether JavaScript monitoring has been refreshed and jumps to other pages. For more related content related to JavaScript monitoring, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!