Click here to Skip to main content
12,747,461 members (38,774 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript ASP.NET4
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 13-Sep-12 22:25pm
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

THE 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);
}
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170215.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100