Clipboard copy function working

CK1820 
Created at Oct 17, 2008 04:28:56 
183   0   0   0  
When copying trackback address or shortcut url on the articles, clipboard function is really useful.
But there are some compatibility issue in browsers.

I checked below function is working on Microsoft Internet Explorer. For supporting Firefox, you need to find another solution.

function copy_clip(meintext){
if (window.clipboardData) {
// the IE-manier
window.clipboardData.setData("Text", meintext);
// waarschijnlijk niet de beste manier om Moz/NS te detecteren;
// het is mij echter onbekend vanaf welke versie dit precies werkt:
} else if (window.netscape) {
// dit is belangrijk maar staat nergens duidelijk vermeld:
// you have to sign the code to enable this, or see notes below
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

// maak een interface naar het clipboard
var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
if (!clip) return;

// maak een transferable
var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
if (!trans) return;

// specificeer wat voor soort data we op willen halen; text in dit geval
trans.addDataFlavor('text/unicode');

// om de data uit de transferable te halen hebben we 2 nieuwe objecten nodig om het in op te slaan
var str = new Object();
var len = new Object();

var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

var copytext=meintext;

str.data=copytext;

trans.setTransferData("text/unicode",str,copytext.length*2);

var clipid=Components.interfaces.nsIClipboard;

if (!clip) return false;

clip.setData(trans,null,clipid.kGlobalClipboard);
}

var msg = alert_msg['url_copy'];
if(typeof(msg)=='undefined'||!msg) return;
alert(msg);
}



Usage sample

<a href=# onclick='copy_clip( "http://kurapa.com"); return false;'>http://kurapa.com</a>



Tags: HTML JSP Optimization Javascript java.util javascript clipboard text string copy Share on Facebook Share on X

▶ NEXT
How to print out text strings on HTML directly ?
  Comments 0
Login for comment
SIMILAR POSTS

How to find the ip address of a client machine from a web application? (created at Mar 21, 2009)

IFRAME Auto Resize in IE, FireFox by Javascript (created at May 02, 2008)

Simple RSS Reader based on javascript (created at Jul 01, 2009)

How to include Javascript from anonther ? (created at Jan 25, 2008)

Javascript based Browser Sniffing (created at Jan 25, 2008)

Javascript based Regular Expression Tester (created at Jan 15, 2008)

Two useful string functions - substr & substring (created at Jan 15, 2008)

Select All, None, Invert checkbox (created at Aug 21, 2009)

How to know the browser name in Javascript ? (updated at Mar 10, 2024)

How to know browser version in javascript ? (created at Aug 22, 2009)

Javascript Prompts (created at Aug 22, 2009)

Javascript Confirmation (created at Aug 22, 2009)

Photo thumbnail viewer implement in javascript (created at Aug 27, 2009)

To get web browser capability (updated at Mar 10, 2024)

Javascript based query parser for AJAX application (created at Aug 27, 2009)

Displays the html tags without rendering on a webpage (created at Aug 27, 2009)

To hide/show area of web page without re-loading a page (created at Aug 27, 2009)

To compare two HTML tables on web page in real time (updated at Mar 10, 2024)

Photoshop script programming in Javascript (created at Sep 14, 2009)

Is there URLEncode function in Javascript ? (created at Oct 11, 2007)

Move window (created at Sep 07, 2007)

How to calculate page loading time ? (created at Sep 07, 2007)

Number Of Vists (created at Sep 07, 2007)

Add Message On Mouse Traling (created at Sep 07, 2007)

Tooltip (created at Sep 07, 2007)

Document Fader (created at Sep 07, 2007)

Book Mark This Site (created at Sep 07, 2007)

Icon Coupled Url (created at Sep 07, 2007)

Button That Will Close The Browser Window (created at Sep 07, 2007)

Button That Will Show The Page's Source (created at Sep 07, 2007)

OTHER POSTS IN THE SAME CATEGORY

How to resize image ? (updated at Dec 19, 2023)

How to set and get sessions on JSP code ? (created at Oct 27, 2008)

How to add a declaration on JSP code ? (created at Oct 27, 2008)

How to include file in JSP ? (created at Oct 27, 2008)

How to print out text strings on HTML directly ? (created at Oct 27, 2008)

UPDATES

Creating a Pinterest-Style Card Layout with Bootstrap and Masonry (created at Apr 24, 2024)

Mastering Excel Data Importation in PHP (updated at Apr 24, 2024)

JSON format control in PHP (updated at Apr 24, 2024)

Equal Height Blocks in Bootstrap with JavaScript (created at Apr 22, 2024)

How to convert integer to text string ? (updated at Apr 22, 2024)

Checking similarity between two strings in PHP (updated at Apr 21, 2024)

Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file (updated at Apr 21, 2024)

How do I determine the client IP type (IPv4/IPv6) in PHP (updated at Apr 16, 2024)

How do I determine the client IP type in Python - IPv4 or IPv6 (updated at Apr 13, 2024)

Getting Started with PyTorch: A Beginner's Guide to Building Your First Neural Network (updated at Apr 09, 2024)

Predicting Buyer Preferences with PyTorch: A Deep Learning Approach (updated at Apr 09, 2024)

Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction (created at Apr 09, 2024)

PyTorch example to Forcast Stock Price based on 10 days Dataset (created at Apr 09, 2024)

Mastering Model Persistence: Saving and Loading Trained Machine Learning Models in Python (created at Apr 08, 2024)

Harnessing the Power of Random Forest Algorithm in Python (created at Apr 08, 2024)

Understanding and Implementing K-Nearest Neighbors (KNN) Algorithm in Python (created at Apr 08, 2024)

Forecasting with Linear Regression and KNN Regression in Python (updated at Apr 07, 2024)

What is 302 Found Redirection in HTTP 1.1? (created at Apr 04, 2024)

Mastering Random Forest Regression: A Comprehensive Guide with Python Examples (updated at Apr 01, 2024)

Python Implementation of Linear Regression (updated at Apr 01, 2024)

Mastering Supervised Machine Learning with Python: A Comprehensive Guide (created at Apr 01, 2024)

Mastering AI: A Beginner's Guide to Python Programming and Beyond (created at Apr 01, 2024)

How do I create animated background for Google Meet? (updated at Mar 28, 2024)

Building a Simple DNS Server in Delphi with TTL Support (created at Mar 16, 2024)

How to force cookies, disable php sessid in URL ? (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records (updated at Mar 16, 2024)

Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records (created at Mar 16, 2024)

Building a Basic DNS Server in PHP/Python: A Beginner's Guide (updated at Mar 15, 2024)

Dynamic DNS Made Easy: Building a Python-Based Solution (created at Mar 15, 2024)

Exploring the Depths of Data Transfer: sendfile vs. kTLS (created at Mar 15, 2024)