p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning HTML, XHTML, CSS, and JavaScript (http://p2p.wrox.com/forumdisplay.php?f=541)
-   -   OnPasteClick doesn't fire when using right click menu? (http://p2p.wrox.com/showthread.php?t=85235)

Ron Howerton October 4th, 2011 01:56 PM

OnPasteClick doesn't fire when using right click menu?
 
My OnPaste event is firing when I paste using the <ctl>+<v> or <shift>+<ins>, but not when using the right click mouse menu. What must I do to get it to recognize when users paste text using the right click mouse menu paste option?

UseLess October 7th, 2011 11:02 AM

Greetings,

The following code works for any means of trying to paste a value into a textbox etc that has the onpaste attribute set:
Code:

// Register onpaste on inputs and textareas in browsers that don't
// natively support it.
(function () {
    var onload = window.onload;

    window.onload = function () {
        if (typeof onload == "function") {
            onload.apply(this, arguments);
        }

        var fields = [];
        var inputs = document.getElementsByTagName("input");
        var textareas = document.getElementsByTagName("textarea");

        for (var i = 0; i < inputs.length; i++) {
            fields.push(inputs[i]);
        }

        for (var i = 0; i < textareas.length; i++) {
            fields.push(textareas[i]);
        }

        for (var i = 0; i < fields.length; i++) {
            var field = fields[i];

            if (typeof field.onpaste != "function" && !!field.getAttribute("onpaste")) {
                field.onpaste = eval("(function () { " + field.getAttribute("onpaste") + " })");
            }

            if (typeof field.onpaste == "function") {
                var oninput = field.oninput;

                field.oninput = function () {
                    if (typeof oninput == "function") {
                        oninput.apply(this, arguments);
                    }

                    if (typeof this.previousValue == "undefined") {
                        this.previousValue = this.value;
                    }

                    var pasted = (Math.abs(this.previousValue.length - this.value.length) > 1 && this.value != "");

                    if (pasted && !this.onpaste.apply(this, arguments)) {
                        this.value = this.previousValue;
                    }

                    this.previousValue = this.value;
                };

                if (field.addEventListener) {
                    field.addEventListener("input", field.oninput, false);
                } else if (field.attachEvent) {
                    field.attachEvent("oninput", field.oninput);
                }
            }
        }
    }
})();

To use it:
Code:

<input type="text" name="id" value="" onpaste="return false;" />

Ron Howerton October 7th, 2011 11:10 AM

Thanks, Useless!


All times are GMT -4. The time now is 11:11 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.