I'm using id in every page to scroll the page to some fixed rate whenever the user is redirecting to another page in my website.
My problem is, user needs to double click the browser's back button to redirect the page to previous page, Thus I need to set double click the back button of a browser whenever the user made a single click the browser back button ..
Thank you
With jquery dblclick method you can that very easily. Some thing like. $ ("p").dblclick (function () { alert ("The paragraph was double-clicked."); }); and If you are using javascript then see following link. The OP is asking how to trigger a double-click, not listen for one. JavaScript-only (no jQuery) was also specified.
But there is a problem that identical events can occur once a user clicks on the refresh button of a browser. So, to grasp whether the refresh button or back button is clicked, we will use the subsequent code. alert ("Browser back button is clicked..."); alert ("Browser refresh button is clicked...");
So let’s try leveraging them to provide back button support for our key Javascript functions. First, we’ll have to create a function to modify the url’s hash value: function set_hash ( new_hash ) { window.location.hash = new_hash; } Call this from within whichever function you want to be tracked by the browser’s back button.
In order to handle back button functionality, we need to come up with a solution that requires server-side effort together with client-side JavaScript code. The concept is... We will store the current page URL in a session variable (say CurrentPageURL) on a page.
Short answer:
document.onmouseover = function() {
//User's mouse is inside the page.
window.innerDocClick = true;
}
document.onmouseleave = function() {
//User's mouse has left the page.
window.innerDocClick = false;
}
window.onhashchange = function() {
if (!window.innerDocClick) {
//Browser back button was clicked
window.history.go(-2);
}
}
Explanation:
The window.history
object contains the URLs that the user has visited in the browser. The window.history.go(x)
function is used to make the browser go to the relative page in history (so in this case -2 to go back 2 pages).
For detecting when the back button in the browser was pressed, I did a quick search and found the solution to this question here about "How to Detect Browser Back Button event - Cross Browser". The accepted answer has a very full and detailed approach to this which I used to get the above snippet, which you should definitely read.
A quick overview of what the above snippet does, is that it uses 2 events to detect whether the user's mouse is within the page document or not. If it is not in the page document, then this means that the user is not going to be clicking on any in-page elements such as hyperlinks etc. which could also be changing the URL. The 'onhashchange' event detects when the location hash changes, and then checks if the user's mouse is within the document, and if not then executes the double back button.
Please note: this method does not cover if the user presses the backspace key (or any other hotkey). The accepted answer does cover a method of doing this using jQuery, which looks as if it could be modified to work in vanilla JavaScript if jQuery is not wanted. This method also probably does not cover devices such as iPads or other tablets and mobile devices as they do not have mouse events. Furthermore, swipe events on a mac's trackpad will most likely not be covered. Not having these devices, it is impossible for me to test them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With