Copy string var to clipboard in browser


Is there a simple way to copy a string var to the clipboard in an ionic browser app.

I think the native clipboard plugin wont work.

In the doc they say

The Clipboard plugin provides Clipboard management for Cordova/PhoneGap that supports iOS, Android, and Windows Phone 8.

But not Windows.


There is a PR that adds Windows support to the ancient base plugin that should also be able to be applied on this plugin: Fork it, apply the PR yourself, install that plugin instead of the ihadeed one. (And if it works create a PR for the ihadeed plugin with the implementation so it can be added by default).


Should it be so easy?

Sorry I’m a beginner in ionic and this would be the first time i use a
cordova plugin.

I think it is a long way for me to get the skills to change a cordova



I recommend this post on medium if you don’t want to use the cordova plugin.


Thank you for your replay. I would try clipboardjs.

I think I have to use the ordova plugin when my app run on mobile and clipboardjs when it runs on Windows as pwa.

I found this solution at stackoverflow. It wroks for me too without an external libary.

copyTextToClipboard(sourceCode) {
	var textArea = document.createElement("textarea"); = 'fixed'; = "0"; = "0"; = '2em'; = '2em'; = "0"; = 'none'; = 'none'; = 'none'; = 'transparent';
	switch (sourceCode){
	  case "providerSource":
	  textArea.value = this.providerSource;
	  case "pageSource":
	  textArea.value = this.providerSource;
	try {
	  var successful = document.execCommand('copy');
	  var msg = successful ? 'successful' : 'unsuccessful';
	  console.log('Copying text command was ' + msg);
	  let toast = this.toastCtrl.create({
		message: 'Copy to clipboard succesfully',
		duration: 1000
	} catch (err) {
	  console.log('Oops, unable to copy');
1 Like

I think direct DOM manipulation, as suggested in the previous post, is best avoided in Angular apps.


Is it possible to add a textarea perhaps in the footer and make it invisible? Than I do not have to add and remove it all the time :slight_smile: