Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..
I am trying to access current clipboard data on onpaste event of textbox.how to access clipboard data in javascript function.i am able to call function on onpaste event but i dont know code to get data from clipboard.provide a soln.thanx
Posted
Comments
Mohibur Rashid 14-Sep-12 4:39am    
JavaScript, don't have any option to read clipboard. You better tell us your plan, we might be able to give you alternate solution

1 solution

THE HTML
HTML
<div id='div' contenteditable='true' onpaste='handlepaste(this, event)'>Paste here</div>

THE JAVASCRIPT

JavaScript
function handlepaste (elem, e) {
    var savedcontent = elem.innerHTML;
    if (e && e.clipboardData && e.clipboardData.getData) {// Webkit - get data from clipboard, put into editdiv, cleanup, then cancel event
        if (/text\/html/.test(e.clipboardData.types)) {
            elem.innerHTML = e.clipboardData.getData('text/html');
        }
        else if (/text\/plain/.test(e.clipboardData.types)) {
            elem.innerHTML = e.clipboardData.getData('text/plain');
        }
        else {
            elem.innerHTML = "";
        }
        waitforpastedata(elem, savedcontent);
        if (e.preventDefault) {
                e.stopPropagation();
                e.preventDefault();
        }
        return false;
    }
    else {// Everything else - empty editdiv and allow browser to paste content into it, then cleanup
        elem.innerHTML = "";
        waitforpastedata(elem, savedcontent);
        return true;
    }
}

function waitforpastedata (elem, savedcontent) {
    if (elem.childNodes && elem.childNodes.length > 0) {
        processpaste(elem, savedcontent);
    }
    else {
        that = {
            e: elem,
            s: savedcontent
        }
        that.callself = function () {
            waitforpastedata(that.e, that.s)
        }
        setTimeout(that.callself,20);
    }
}

function processpaste (elem, savedcontent) {
    pasteddata = elem.innerHTML;
    //^^Alternatively loop through dom (elem.childNodes or elem.getElementsByTagName) here

    elem.innerHTML = savedcontent;

    // Do whatever with gathered data;
    alert(pasteddata);
}
 
Share this answer
 
Comments
Vijay Walunj,Navi mumbai 14-Sep-12 5:21am    
I have tried this code but it doesn't work.i am using one asp textbox.copy data from excel,notepad,miscrosoftword and paste it on my textbox which is in my web page.onpaste event call javascript function but e && e.clipboardData && e.clipboardData.getData condition false b'coz e.clipboardData is undefined.provide soln for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900