Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Copy href onClick


  • Please log in to reply
1 reply to this topic

#1 rsnider19

rsnider19

    CC Regular

  • Just Joined
  • PipPipPip
  • 34 posts

Posted 15 November 2010 - 10:44 AM

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?
  • 0

#2 nomaden

nomaden

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 15 November 2010 - 04:02 PM

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

  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download