JavaScript achieves perfect compatibility with the Multi-browser copy Function Code

Source: Internet
Author: User

JavaScript achieves perfect compatibility with the Multi-browser copy Function Code

 JavaScript achieves perfect compatibility with the Multi-browser copy Function Code

In the past two days, when we were working on the Web Front-end, we had to use JavaScript to implement text replication. After some tests, we finally achieved it. If you need it, you can refer to it.

Share a piece of code that uses JavaScript to implement the copy function, compatible with multiple browsers, compatible with IE and Firefox browsers.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> JavaScript copy code, compatible with multiple browsers </title>

</Head>

<Script language = "javascript">

CopyValue = function (strValue ){

If (isIE ()){

ClipboardData. setData ("Text", strValue );

Alert ("You have copied this address ");

} Else {

Copy (strValue );

Alert ("the content has been copied! ");

}

}

Function isIE (number ){

If (typeof (number )! = Number ){

Return !! Document. all;

}

}

Function copy (text2copy ){

Var flashcopier = 'flashcopier ';

If (! Document. getElementById (flashcopier )){

Var divholder = document. createElement ('div ');

Divholder. id = flashcopier;

Document. body. appendChild (divholder );

}

Document. getElementById (flashcopier). innerHTML = '';

Var divinfo = '<embed src = "http://files.jb51.net/demoimg/200910/_clipboard.swf" FlashVars = "clipboard =' + text2copy + '" width = "0" height = "0" type = "application/x-shockwave -flash "> </embed> '; // here is the key

Document. getElementById (flashcopier). innerHTML = divinfo;

}

</Script>

<Div class = "phoinfo">

Texture address: <input name = "txtPhotoPath" value = "www.daimajiayuan.com" id = "txtPhotoPath" type = "text" size = "65"/>

<Input type = "button" name = "btnCopy" id = "btnCopy" onClick = "copyValue ('www .daimajiayuan.com ');" value = "copy"/>

</Div>

</Body>

</Html>

The above is all the content shared in this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.