简体   繁体   中英

Uncaught TypeError: Cannot read property 'setData' of undefined

i wrote Javascript that copies the data from textbox to the clipboard:

TextBox:

<asp:TextBox ID="contNoTxtBox" runat="server" Width="182px" style="height: 22px"></asp:TextBox>

Javascript:

function CopyToClipBoard()
{
    var text = document.getElementById('contNoTxtBox').innerHTML;                       
    window.clipboardData.setData('Text', text);                       
}

and i call it in HTML button the following way:

<input id="CopyButton" type="button" value="Copy" onclick="CopyToClipBoard()" /><br />

However i get the following error:

Uncaught TypeError: Cannot read property 'setData' of undefined

Why am i getting this type pf error?

Referring to http://help.dottoro.com/ljctuhrg.php your

window.clipboardData.setData('Text', text);

is compatible with InternetExplorer only. There are alternative ways to implement this using Chrome etc. shown in their examples.

Aspx code:
<head>
 <script language="javascript" type="text/javascript">

               function pasteContent() {
                   document.getElementById('ClipboardContent').value = document.execCommand('copy');//     window.clipboardData.getData('Text');
                   return (true);
               }
               var elem3 = document.getElementById("elem");
               document.addEventListener("click", function () {
                   document.getElementById("demo").innerHTML = "Hello World";
               });
               elem3.addEventListener('copy', function (e) {

                   e.preventDefault();
                   if (e.clipboardData) {
                       e.clipboardData.setData('text/plain', 'custom content from click');
                   } else if (window.clipboardData) {
                       window.clipboardData.setData('Text', 'custom content from click');
                   }

               });
               </script>    <script runat="server">
               protected override void OnInit(EventArgs e) { Page.ClientTarget =    "uplevel"; base.OnInit(e);
               } 
    </script>

</head>
 <body>

                <asp:Button ID="ReloadCtl" runat="server" Text="Paste" OnClick="ReloadCtl_Click"
                    OnClientClick="return pasteContent();" />
         <asp:HiddenField ID="ClipboardContent" runat="server" ClientIDMode="Static" /> </body>
    C# Code:
         protected void ReloadCtl_Click(object sender, EventArgs e)
                {
        if (!string.IsNullOrEmpty(this.ClipboardContent.Value))
                    {

        DataTable dt1 = DataMapper.GetDataTable(this.ClipboardContent.Value, false);
        }


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM