버튼 클릭시 현재 url을 복사하고싶습니다..

버튼 클릭시 현재 url을 복사하고싶습니다..

QA

버튼 클릭시 현재 url을 복사하고싶습니다..

본문


<?
$url  = $_SERVER['SERVER_NAME'].'/'.$_SERVER['PHP_SELF'];
?>
<button onclick="copy_to_clipboard()">Select</button>
<br/>
<input type="text" id="myInput" value="<?php echo $url; ?>">
<script>
function copy_to_clipboard() {
  $("#myInput").select();
  document.execCommand("copy");
  alert('복사되었습니다.');
}
</script>

 

이런식으로 현재 url을 input에 넣어서 복사하는데 까지는 성공했습니다!

그런데 저는 input이 필요없어서 보여주기싫어서...

display:none;을 하니

복사가 되지않고...

그냥 단순하게 버튼을 클릭하면

현재 url을 복사하고싶습니다.

조언좀 부탁드립니다.ㅠㅠ

이 질문에 댓글 쓰기 :

답변 4

<input type="text" id="myInput" value="<?php echo $url; ?>">

대신

<input type="button"  id="myInput" value="url 복사 ">

 

스크립트를

$("#myInput").attribute("value","<?php echo $url; ?>")

$("#myInput").select();

document.execCommand("copy");

$("#myInput").attribute("value","url 복사")

이런식으로 하면 안될까요?

input type="text" 대신에 input type="hidden" 을 사용하시면 되지 않나요?

input type을 text 말고 hidden 으로 해보세요

답변을 작성하시기 전에 로그인 해주세요.
전체 10,628
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT