共用方式為


SharePoint 2010 | Copy text to clipboard

I was recently working on a web part which had to copy the data in the datagrid to clip board. Servers side code tends to get messy and I found an easy way to copy to client's clip board using Javascript.

<Javascript code>

The javascript for copying to clip board is

    function copy_to_clipboard(text) 
    { 
        if(window.clipboardData) 
        { 
        window.clipboardData.setData('text',text); 
        } 
        else 
        { 
            var clipboarddiv=document.getElementById('divclipboardswf'); 
        if(clipboarddiv==null) 
        { 
           clipboarddiv=document.createElement('div'); 
               clipboarddiv.setAttribute("name", "divclipboardswf"); 
           clipboarddiv.setAttribute("id", "divclipboardswf"); 
           document.body.appendChild(clipboarddiv); 
        } 
            clipboarddiv.innerHTML='<embed src="clipboard.swf" FlashVars="clipboard='+ 
    encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>'; 
        } 
        alert('The text is copied to your clipboard...'); 
        return false; 
    }

</Javascript code>

Thereafter you can register the javascript in the web part.

Comments

  • Anonymous
    December 23, 2014
    This is only supported in IE. Other browsers don't offer copy to clipboard functionality.