javascript-events

Why does calling Window.scroll() give a trusted event?

杀马特。学长 韩版系。学妹 提交于 2019-12-18 22:15:29
问题 I have a Chrome extension that needs to produce human-like mouse and keyboard behavior (specifically, generate events that have a isTrusted value of true ). I can do everything I need except for scrolling with the chrome.debugger APIs. But it seems that the Window.scroll() method is sufficient for this purpose up to Chrome 52 and Firefox 48.0a1. This can be observed by attaching an event listener to the page as follows: document.addEventListener("scroll", function (event) { console.log("event

How can I remove a click event from an element in Javascript?

一笑奈何 提交于 2019-12-18 21:19:24
问题 I have a <div> element that has a click event attached to it using the following code: var id = "someId"; var elem = document.getElementById("elemId"); elem.addEventListener("click", function() { someFunction(id); }, false); At a later point I copy the element and add it to another part of the DOM , but need to first remove the click event var elem = document.getElementById("elemId"); elem.removeEventListener("click", ???? , false); I'm not sure how to reference the listener and so far

addEventListener firing multiple times for the same handle when passing in arguments with anonymous function

浪子不回头ぞ 提交于 2019-12-18 21:11:16
问题 For some reason, the event listener is firing twice for each element when passing arguments into an anonymous function. I.e., the click event on element el will register once and, thus, fire once. el.addEventListener("click", handle, false); el.addEventListener("click", handle, false); But if I want to pass my own arguments to it, it will register and fire twice. el.addEventListener("click", function() { handle(event, myArgument); }, false); el.addEventListener("click", function() { handle

Function for mouse near an element in jQuery

倾然丶 夕夏残阳落幕 提交于 2019-12-18 19:03:51
问题 I want to track and show a tooltip when the mouse is near a table head element. It works with the mouseenter event, but I want show the tooltip before mouseenter , when it gets near. Also I want remove the tooltip after mouseout some distance from the table head. This is my code. $('thead').mouseenter(showtooltip); $('thead').mouseout(removetooltip); How can I do this with jQuery? 回答1: This works. The first parameter can be any jQuery object. The second parameter is the nearness to the object

Distinguish onbeforeunload for File Download vs Page Change

雨燕双飞 提交于 2019-12-18 18:43:27
问题 I have an onbeforeunload event that's supposed to get triggered any time a user goes to a new page. It works well enough, but I've found that it also gets triggered in Chrome any time a user downloads a file from the page they're on. I'd like to be able to tell if the event is getting fired because it's being triggered by a file download. What's the best way to do that? EDIT: As a clarification, I don't own the site that I'm listening to onbeforeunload on. The event is listened to by a

Distinguish onbeforeunload for File Download vs Page Change

半城伤御伤魂 提交于 2019-12-18 18:43:06
问题 I have an onbeforeunload event that's supposed to get triggered any time a user goes to a new page. It works well enough, but I've found that it also gets triggered in Chrome any time a user downloads a file from the page they're on. I'd like to be able to tell if the event is getting fired because it's being triggered by a file download. What's the best way to do that? EDIT: As a clarification, I don't own the site that I'm listening to onbeforeunload on. The event is listened to by a

Catching the specific javascript code being executed onClick

大憨熊 提交于 2019-12-18 18:38:11
问题 I am working on a site that has loads of legacy javascript and jquery includes and there is no documentation to show what is happening when. I have a specific problem to fix and I cannot find the relevant code that is being executed when a button is clicked. To save me from trawling through (and making sense of) hundreds of lines of legacy script, is there a feature, possibly in Firebug, that will identify what script is being executed when I click on a button? Thanks for your help. 回答1: I

jQuery hover not triggered when element is programatically moved under the mouse

妖精的绣舞 提交于 2019-12-18 16:48:46
问题 I have an image with a hover effect (higher opacity when mouse is over it). It works as desired when the mouse moves in and out. However, the image itself is moving (I'm periodically changing the css attribute top). When the mouse does not move and the image moves under the mouse cursor, no related events are triggered. That means, the hover functions are not called. I also tried using the mouseenter and mouseleave events instead, but they don't work either. What would be a good approach to

Printing contents of another page

女生的网名这么多〃 提交于 2019-12-18 15:16:09
问题 In the following link <a href=\"#\" onclick=javascript:print(\"\") style=\"color:blue;\">Print</a>" <script> function print() { //How to print the contents of another page } 回答1: I think you can print the contents of the current page not external page. 回答2: I know it´s an old question, but you can do it like this: function printExternal(url) { var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0'); printWindow.addEventListener('load',

How to check if a Textarea is empty in Javascript or Jquery?

喜夏-厌秋 提交于 2019-12-18 14:19:12
问题 How do i check if a textarea contains nothing? I tryed with this code if(document.getElementById("field").value ==null) { alert("debug"); document.getElementById("field").style.display ="none"; } But it doesnt do what i expect. I expect that it should appear a messagebox "debug" and that the textarea is not shown. How can i fix that issue? 回答1: You wanna check if the value is == "" , not NULL . if(document.getElementById("field").value == '') { alert("debug"); document.getElementById("field")