1function (container_id, as_html) {
2 /*
3 isEmpty = function (val) {
4 return (val === undefined || val == null || val.length <= 0) ? true : false;
5 };
6 */
7 as_html = isEmpty(as_html) ? false : true;
8
9 //console.log('copyToClipboard: ' + container_id);
10 var elem = document.getElementById(container_id);
11
12 // create hidden text element, if it doesn't already exist
13 var targetId = "_hiddenCopyText_";
14 var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
15 var origSelectionStart, origSelectionEnd;
16 if (isInput) {
17 // can just use the original source element for the selection and copy
18 target = elem;
19 origSelectionStart = elem.selectionStart;
20 origSelectionEnd = elem.selectionEnd;
21 //console.log ('_is_input = true');
22 } else {
23 // must use a temporary form element for the selection and copy
24 //console.log ('_is_input = false, creating a textarea = ' + targetId);
25 target = document.getElementById(targetId);
26 if (!target) {
27 var target = document.createElement("textarea");
28 target.style.position = "absolute";
29 target.style.left = "-9999px";
30 target.style.top = "0";
31 target.id = targetId;
32 document.body.appendChild(target);
33 }
34
35 var content = $.trim(as_html ? $(elem).html() : $(elem).text());
36 $(target).text( content);
37 console.log ('text_content', content);
38 }
39 // select the content
40 var currentFocus = document.activeElement;
41 target.focus();
42 target.setSelectionRange(0, content.length);
43
44 // copy the selection
45 var succeed;
46 try {
47 succeed = document.execCommand("copy");
48 } catch (e) {
49 succeed = false;
50 }
51 // restore original focus
52 if (currentFocus && typeof currentFocus.focus === "function") {
53 currentFocus.focus();
54 }
55
56 if (isInput) {
57 // restore prior selection
58 elem.setSelectionRange(origSelectionStart, origSelectionEnd);
59 } else {
60 // clear temporary content
61 $(target).text('');
62 }
63
64 return succeed;
65 }