Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
333 views
in Plugins by
hello every body

i want method by it i can put button in the end every answer and question , when the visitor click on this button Copies the answer or question link.
Q2A version: 1.83

1 Answer

–1 vote
by
edited by

I tried but it's not perfect.

public function q_view_buttons($q_view) {

$page_url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$this->output ('<input type="text" value="'.$page_url.'" id="myInput" style="width:1px; height:1px">');
<button type="button" onClick="myFunction(); return false;" style="margin-left:-10px;z-index:999;">
$this->output ('<script src="../script/copylink.js"></script>');

        qa_html_theme_base::q_view_buttons($q_view);
    }

And the script:

function myFunction() {
  /* Get the text field */
  var copyText = document.getElementById("myInput");
  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/

  /* Copy the text inside the text field */
  document.execCommand("copy");

  /* Alert the copied text */
 alert("Link copied: " + copyText.value);
}

Not sure why it doesn't work on mobile phones!!!

Btw, it seems that Firefox doesn't allow URL to be copied from hidden input.

Who can detect how this site has a nice "copy link" function ? https://www.proprofs.com/discuss/q/54163/what-is-the-full-form-of-ok

...