日期:2014-05-16  浏览次数:20405 次

js在不同浏览器中复制代码的实现

最近在写一个复制相关代码到剪贴板的功能!

?

如果只是IE浏览器的话,可以利用window.clipboardData这个对象,相关js代码:

?

?

window.clipboardData.clearData();
		var couponResult = window.clipboardData.setData("couponResult",
				couponResultHtml);
		if (couponResult) {
			alert("优惠券代码拷贝成功!");
		} else {
			alert("优惠券代码拷贝失败!");
		}
?

?

但这个对象是只针对IE的。。如果是其它的浏览器呢,则会用不了,这里提供两种相关功能的方法

1.

?

if (window.clipboardData) {
		window.clipboardData.clearData();
		var couponResult = window.clipboardData.setData("couponResult",
				couponResultHtml);
		if (couponResult) {
			alert("优惠券代码拷贝成功!");
		} else {
			alert("优惠券代码拷贝失败!");
		}
	} else if (navigator.userAgent.indexOf("Opera") != -1) {
		window.location = couponResultHtml;
	} else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager
					.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1']
				.createInstance(Components.interfaces.nsIClipboard);
		if (!clip)
			return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1']
				.createInstance(Components.interfaces.nsITransferable);
		if (!trans)
			return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"]
				.createInstance(Components.interfaces.nsISupportsString);
		var copytext = couponResultHtml;
		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);
	}

?

?这个需要对浏览器进行设置,在浏览器栏中输入:about:config找到signed.applets.codebase_principal_supporty设置为true

?

?

2.得用flash的功能,现在网上有人写了一个ZeroClipboard.js,ZeroClipboard.swf

只要将这两文件作相关改动就行了:可以实现任意浏览器的复制,当然要浏览器支持flash插件才行!

ZeroClipboard.js相关代码:(此有经过改动),因为项目中有用到jquery,与其中的$操作符有功能上的冲突,所以改成了_(下划线),

?

// Simple Set Clipboard System
// update 2011-04-20
// Author: Eric_wang  

var ZeroClipboard = {
	
	version: "1.0.6",
	clients: {}, // registered upload clients on page, indexed by id
	moviePath: rootStaticPath+'/js/activity/coupon/ZeroClipboard.swf', // URL to movie
	nextId: 1, // ID of next movie
	
	_: function(thingy) {
		// simple DOM lookup utility function
		if (typeof(thingy) == 'string') thingy = document.getElementById(thingy);
		if (!thingy.addClass) {
			// extend element with a few useful methods
			thingy.hide = function() { this.style.display = 'none'; };
			thingy.show = function() { this.style.display = ''; };
			thingy.addClass = function(name) { this.removeClass(name); this.className += ' ' + name; };
			thingy.removeClass = function(name) {
				var classes = this.className.split(/\s+/);
				var idx = -1;
				for (var k = 0; k < classes.length; k++) {
					if (classes[k] == name) { idx = k; k = classes.length; }
				}
				if (idx > -1) {
					classes.splice( idx, 1 );
					this.className = classes.join(' ');
				}
				return this;
			};
			thingy.hasClass = function(name) {
				return !!this.className.match( new RegExp("\\s*" + name + "\\s*") );
			};
		}
		return thingy;
	},
	
	setMoviePath: function(path) {
		// set path to ZeroClipboard.swf
		this.moviePath = path;
	},
	
	dispatch: function(id, eventName, args) {
		// receive event from flash movie, send to client		
		var client = this.clients[id];
		if (client) {
			client.receiveEvent(eventName, args);
		}
	},
	
	register: function(id, client) {
		// register new client to receive events
		this.clients[id] = client;
	},
	
	getDOMObjectPosition: function(obj, stopObj) {
		// get absolute coordinates for dom element
		var info