Jump to content

Copy href onClick

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
rsnider19

rsnider19

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Is there a way I can have a user click a link on my site and have it copy to there clipboard. I saw examples using execCommand("Copy"), but that was to copy textareas. These urls would be dynamically generated. Or is there a way to have it so when they click a url (which would normally open in a new tab), the browser prevents it from opening the new tab and copies the url instead?

#2
nomaden

nomaden

    Newbie

  • Members
  • PipPip
  • 10 posts
You could use return false to stop browser execute the default behavior or using # as anchor href. This is very simple example to suppress anchor default behavior. Clipboard copying is one of ultimate problem for the century :D, and code here is only work for IE.


<script type="text/javascript">

	function copy_to_clipboard(element)

	{

		if( window.clipboardData && clipboardData.setData )

		{

			clipboardData.setData("Text", element.innerHTML);

		}

		return false;

	}

</script>

<a href="http://forum.codecall.net" onclick="return copy_to_clipboard(this)">CodeCall Forum</a>

or

<a href="#" onclick="copy_to_clipboard(this)">Copy to clipboard</a>


Edited by nomaden, 15 November 2010 - 04:04 PM.
typo