Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy current url button - Javascript

Tags:

javascript

okay it might be something simple or completely wrong however I'm trying to create a button to copy current URL to clipboard, this is what I currently have, I'm not too familiar with javascript so this is from a few sources mixed together and hoping for the best.

<button onclick="CopyLink()">Click me</button>
<script>
function CopyLink() {
window.clipboardData.setData("Text", location.href);
}
</script>

Any help would be great thanks!

like image 956
Paul Smith Avatar asked May 18 '16 19:05

Paul Smith


People also ask

How to copy the current URL to the clipboard with JavaScript?

To copy the current URL to the clipboard with JavaScript, we can call navigator.clipboard.writeText with window.location.href. to add a button. We get the button with document.querySelector. Then we set the button.onclick property to a function that calls navigator.clipboard.writeText with window.location.href.

How to get the URL of the current page in HTML?

We get the button with document.querySelector. Then we set the button.onclick property to a function that calls navigator.clipboard.writeText with window.location.href. window.location.href has the URL of the current page, so the URL will be copied to the clipboard with the copy button is clicked.

How to get current URL in address bar in JavaScript?

JavaScript suggests a bunch of methods that help to get the current URL displayed at the address bar. All of the methods use the Location object (contains information about the current URL), which is a property of the Window object (provides current page address (URL) and redirects the browser to a new page).

How do I copy a link on a button click?

Using the function copyTextToClipboard from this answer, you can copy the link on a button click like so: function copyTextToClipboard (text) { var textArea = document.createElement ("textarea"); // // *** This styling is an extra step which is likely not required.


1 Answers

Using the function copyTextToClipboard from this answer, you can copy the link on a button click like so:

function copyTextToClipboard(text) {
  var textArea = document.createElement("textarea");

  //
  // *** This styling is an extra step which is likely not required. ***
  //
  // Why is it here? To ensure:
  // 1. the element is able to have focus and selection.
  // 2. if element was to flash render it has minimal visual impact.
  // 3. less flakyness with selection and copying which **might** occur if
  //    the textarea element is not visible.
  //
  // The likelihood is the element won't even render, not even a flash,
  // so some of these are just precautions. However in IE the element
  // is visible whilst the popup box asking the user for permission for
  // the web page to copy to the clipboard.
  //

  // Place in top-left corner of screen regardless of scroll position.
  textArea.style.position = 'fixed';
  textArea.style.top = 0;
  textArea.style.left = 0;

  // Ensure it has a small width and height. Setting to 1px / 1em
  // doesn't work as this gives a negative w/h on some browsers.
  textArea.style.width = '2em';
  textArea.style.height = '2em';

  // We don't need padding, reducing the size if it does flash render.
  textArea.style.padding = 0;

  // Clean up any borders.
  textArea.style.border = 'none';
  textArea.style.outline = 'none';
  textArea.style.boxShadow = 'none';

  // Avoid flash of white box if rendered for any reason.
  textArea.style.background = 'transparent';


  textArea.value = text;

  document.body.appendChild(textArea);

  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copying text command was ' + msg);
  } catch (err) {
    console.log('Oops, unable to copy');
  }

  document.body.removeChild(textArea);
}

function CopyLink() {
  copyTextToClipboard(location.href);
}
<button onclick="CopyLink()">Click me</button>
like image 142
rphv Avatar answered Sep 29 '22 03:09

rphv