top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is there any way to get Clipboard data using java script ?

+1 vote
517 views

Please provide some sample code.I need to take copied data's from clipboard .Any one help me to solve this .

posted Jul 14, 2014 by Sidharth Malhotra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You can use

window.clipboardData.getData('Text')

It will work in some browsers.

However, in other browser you may need to use flash to get the content, since there is no standard interface to access the clipboard. May be you can have try this plugin Zero Clipboard

Also,By default JavaScript is not allowed to read or set your clipboard data for security and privacy reasons. This is because websites scripts can erase and replace what you currently have in your clipboard (data loss issue) and they can read whatever you have in your clipboard (security and privacy issue)

Read more for javascript clipboard grant acess
http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard

answer Aug 12, 2014 by Pankaj Deshmukh
Similar Questions
0 votes

Please give the example for both.

+2 votes

I know it is possible via IE (ActiveX objects).
Can we do this with all browsers ? (Specially in FireFox)

0 votes

I need to execute some function while pasting in web page. Can any tell me to solve this issue?This can be possible in Java Script or Jquery?

0 votes

Also,tell me what are the types of framework are there ?

0 votes

I tried the following code.Both onpaste() and Oncopy() will trigger when user trying to copy or paste.

But my problem is while copying something window.getSelection() not working.

Window.getSelection() is used to get the selected data.

I mean before copying something user need to select some values.So,using this window.getselection() we will get the value what they are selected.

Here, my problem is window.getselection() will work in oncopy but not working in onpaste.

Check the below examples and help me to solve the problem.

Also ,is there any alternative method also please let me know.I also tried window.clipboardData.But not getting the solution for this.

<!doctype html>
<html>
<head>
<style>
    textarea {
        width:500px;
        height:200px;
        border:2px solid #999;
    }
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script type="text/javascript">

        function OnPaste () {                                                                       
            var selection = window.getSelection();          
            alert(selection.toString());            
        }                                       
        function OnCopy() {         
            var selection = window.getSelection();
            alert(selection.toString());
         }

// Call the OnCopy function while user trying to copy 
document.oncopy = OnCopy;

// Call the OnPaste function while user trying to Paste
document.onpaste = OnPaste;
 </script>

</head>
<body>
    Select some text, copy it to the clipboard and try to paste it to the following fields:
    <br /><br />
    <textarea size="40" value="Copy and Paste operation is enabled">
...