javascript copy link

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

javascript copy link

Post by m3rajk »

trying to make a link that will copy. not sure what's wrong. example link: http://24.91.157.113/findyourdesire/profile.php?un=neo
under the pictures, the link to this profile link.
here's the code for those that don't care to look (as sent to the browser from the php):
link:

Code: Select all

<li>Link Directly to this profile: <a name="#Neo" href="#Neo" onClick="toClipboard();">http://www.FindYourDesire//findyourdesire/profile.php?un=Neo</a></li>
javascript:

Code: Select all

<script language="javascript">
function toClipboard()&#123;
  var text="http://www.FindYourDesire//findyourdesire/profile.php?un=Neo";
  var copyText=text.createTextRange();
  
  return confirm("Do you want to copy the link http://www.FindYourDesire//findyourdesire/profile.php?un=Neo to your clipboard?
(this only works for M$IE");
  copyText.execCommand("Copy");
  alert("If using M$IE, then you have just copied the link, http://www.FindYourDesire//findyourdesire/profile.php?un=Neo to your clipboard.");
&#125;
      </script>
thanx in advance for any help, as you can see i do understand it will only work in ie once i get it working. testing in both M$IE and mozilla 1.3.1. i just want the confirm to come up in non-M$IE browsers (umm... 2 notes: one: i took care fo the // issue and two: even though i have the url, i don't have a host yet, so the link (created by php) isn't really working. (the one to the example does when the server is ON, and my rents have a tendancy to turn it off) :-P)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

it won't work this way
http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/obj_textrange.asp
o retrieve a text range object, apply the createTextRange method to a body, button, or textArea element or an input element that has TYPE text.
confirm() returns true or false depending on the user's choice but you do not want the function to return the value at this place
return confirm("[...]");
if probably meant as

Code: Select all

if (confirm("..."))
&#123;
	
&#125;
try this one

Code: Select all

<html>
	<head>
		<script type="text/javascript">
			function toClipboard(objInputLink)
			&#123;
  			if ( confirm("Do you want to copy the link "+objInputLink.value+" to your clipboard?\n(this only works for M$IE") )
  			&#123;
  				var copyText=objInputLink.createTextRange();
  				copyText.execCommand("Copy");
  				alert("If using M$IE, then you have just copied the link "+objInputLink.value+" to your clipboard.");
  			&#125;
			&#125;
		</script>
	</head>
	<body>
		<input type="text" readonly="readonly" value="http://www.php.net" readonly="readonly" onClick="toClipboard(this);" />
		<input type="text" value="http://forums.devnetwork.net/portal.php" onClick="toClipboard(this);" />
	</body>
</html>
Post Reply