How can I set an event listener to listen to a text box from the HTML Source code using JavaScript? - Stack Overflow

Currently developing a Firefox Addon in JavaScript. It scans a webpage for specific things like email a

Currently developing a Firefox Addon in JavaScript. It scans a webpage for specific things like email addresses. Is there a way of being able to get it to read the source code of a website and set an event listener to a text box which can then detect whether a user is entering an email address in a text box on any website. Not sure whether this is possible in JavaScript as I am new to it. I need to be able to display an alert if the user types in an email address on a website but as this will be running from a Firefox Addon. Almost like it would display if you were using it for validation purposes on a website. But as this is from a Firefox Addon it doesn't quite work the same way. I have looked at GreaseMonkey but its quite confusing and when trying to find specifically whether it could be done I have got stuck.

How could I implement this would be great or even whether it is possible.

Currently developing a Firefox Addon in JavaScript. It scans a webpage for specific things like email addresses. Is there a way of being able to get it to read the source code of a website and set an event listener to a text box which can then detect whether a user is entering an email address in a text box on any website. Not sure whether this is possible in JavaScript as I am new to it. I need to be able to display an alert if the user types in an email address on a website but as this will be running from a Firefox Addon. Almost like it would display if you were using it for validation purposes on a website. But as this is from a Firefox Addon it doesn't quite work the same way. I have looked at GreaseMonkey but its quite confusing and when trying to find specifically whether it could be done I have got stuck.

How could I implement this would be great or even whether it is possible.

Share Improve this question asked Mar 1, 2013 at 9:18 user2055933user2055933
Add a ment  | 

3 Answers 3

Reset to default 2

You could do something like this:

window.onload = function () {
    var inputs = document.getElementsByTagName('input');
    for (i = 0; i < inputs.length; i++) {
        inputs[i].onkeypress = function () {
            if (this.value.match(/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/)) {
                // do something
                alert('Looks like an email address!');
            }
        };
    }
};

Try it here: http://jsfiddle/samliew/49cny/

You can add a event listener like:

Syntax

target.addEventListener(type, listener[, useCapture]);
target.addEventListener(type, listener[, useCapture, aWantsUntrusted Non-standard]); // Gecko/Mozilla only

From https://developer.mozilla/en/docs/DOM/element.addEventListener

For example

document.getElementById('inputid').addEventListener("keypress", function(){/* Your code */}, false);

You would use the jQuery .keyPress() function Below is an example...Where #target is the ID of the Textbox.

var i=0;
$( "#target" ).keypress(function() {
i++;
console.log("Kep Pressed: " + i);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745047883a4608206.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信