Home > OS >  copy url{Little part} to clipboard
copy url{Little part} to clipboard

Time:07-13

I am using given below code to copy url, but i am thinking about something different like in given example -- https://oauth2.example.com/code?state=state_parameter_passthrough_value&code=4/0AdQt8qg5KBjhjcHixOZMVfpO4cl_UB8NpoV9-GxBzeiI8xCH_8oqzmYhXvGrAx1SyEFYyQ&scope=https://www.googleapis.com/auth/drive.metadata.readonly

This is the complete url. But i wanted to only copy in clipboard THis ---

4/0AdQt8qg5KBjhjcHixOZMVfpO4cl_UB8NpoV9-GxBzeiI8xCH_8oqzmYhXvGrAx1SyEFYyQ

If this is possible, please somebody guide me.

<!DOCTYPE html>
<html>
<body>

<button onclick="copyToClipboard()">Copy Link</button>

<script>
function copyToClipboard(text) {
var inputc = document.body.appendChild(document.createElement("input"));
inputc.value = window.location.href;
inputc.focus();
inputc.select();
document.execCommand('copy');
inputc.parentNode.removeChild(inputc);
alert("URL Copied.");
}
</script>

</body>
</html>

CodePudding user response:

Get url query string

const queryString = window.location.search;

Get code parameter value

const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code');

Save code parameter into clipboard

navigator.clipboard.writeText(code);
  • Related