Answer the question
In order to leave comments, you need to log in
jQuery: how to track the transition to another page by executing JavaScript code?
There is a need to add something to the address that leads to another page. Those. the user clicked on the link or clicked on the button, and JS adds one more parameter (token) to the URL - actually fighting XSS.
How to track regular <a, <form, etc. understandably. But what if the transition is carried out on the execution of onclick? It seems like you need to track the “change” event for window.location, but as I understand it, this cannot be done.
Neither I nor my colleagues are specialists in JS, we know it in so far as we scratch our turnips and cannot think of it. The application is awesome in size and written very crookedly, we will look for all occurrences of JavaScript until the second coming.
No one will help with scary knowledge?
Answer the question
In order to leave comments, you need to log in
Maybe the jQuery 'unload' event will help you: api.jquery.com/unload/ ?
In each place where there is a window.location change, add what you need.
In Chrome,
window.onbeforeunload = function (event) {console.log(event.target.URL); return false;}
No, it doesn't help, we tried it - window.location remains the same. Here one pretzel writes about event handling, as it were:
But it works (and the example is the same) only on ordinary links like <a, it doesn’t work on JavaScript. In addition, he writes there that As you know, window.location is a Javascript object that contains information about the URL including hash and search strings. Those. it's not an HTML object, but Javascript, i.e. like how JQuery cannot be configured to process it?
$( window.location ).trigger(
"change",
{
currentHref: strLocation,
currentHash: fnCleanHash( strHash ),
previousHref: strPrevLocation,
previousHash: fnCleanHash( strPrevHash )
}
);
There is a standard callback - window.onbeforeunload. I think you can change where to go there.
jquery plugin here: plugins.jquery.com/plugin-tags/windowonbeforeunload
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question