How can I capture the right-click event in JavaScript?

Use the oncontextmenu event. Here’s an example: And using event listeners (credit to rampion from a comment in 2011): Don’t forget to return false, otherwise the standard context menu will still pop up. If you are going to use a function you’ve written rather than javascript:alert(“Success!”), remember to return false in BOTH the function AND the oncontextmenu attribute.

Why onbeforeunload event is not firing

The onbeforeunload event is not cancel-able, because of security reasons, but if an event handler function for the onbeforeunload event returns a string value, this text will be shown in a confirmation dialog box, where the user can confirm whether he wants to stay or leave the current page. Note that event listeners cannot be registered for the onbeforeunload event with the addEventListener and attachEvent methods (only … Read more

How to get scrollbar position with Javascript?

You can use element.scrollTop and element.scrollLeft to get the vertical and horizontal offset, respectively, that has been scrolled. element can be document.body if you care about the whole page. You can compare it to element.offsetHeight and element.offsetWidth (again, element may be the body) if you need percentages.

window.onload vs $(document).ready()

The ready event occurs after the HTML document has been loaded, while the onload event occurs later, when all content (e.g. images) also has been loaded. The onload event is a standard event in the DOM, while the ready event is specific to jQuery. The purpose of the ready event is that it should occur as early as possible after the document has loaded, so … Read more

tech