-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccopy.js
More file actions
46 lines (41 loc) · 1.28 KB
/
ccopy.js
File metadata and controls
46 lines (41 loc) · 1.28 KB
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
39
40
41
42
43
44
45
46
// Copy to clipboard with tooltip
// <a href="#" onclick="copyfieldvalue(event, 'sometextfield');return false">Copy</a>
// load <script>createtooltip();</script> somewhre on the page
var tooltip,
hidetooltiptimer
function createtooltip() {
tooltip = document.createElement('div')
tooltip.style.cssText = 'position:absolute; background:black; color:white; padding:4px;z-index:10000;'
+ 'border-radius:2px; font-size:12px;box-shadow:3px 3px 3px rgba(0,0,0,.4);'
+ 'opacity:0;transition:opacity 0.3s'
tooltip.innerHTML = 'Copied!'
document.body.appendChild(tooltip)
}
function showtooltip(e) {
var evt = e || event
clearTimeout(hidetooltiptimer)
tooltip.style.left = evt.pageX + 25 + 'px'
tooltip.style.top = evt.pageY + 15 + 'px'
tooltip.style.opacity = 1
hidetooltiptimer = setTimeout(function() {
tooltip.style.opacity = 0
}, 500)
}
function copyfieldvalue(e, id) {
var field = document.getElementById(id)
field.focus()
field.setSelectionRange(0, field.value.length)
var copysuccess = copySelectionText()
if (copysuccess) {
showtooltip(e)
}
}
function copySelectionText() {
var copysuccess
try {
copysuccess = document.execCommand("copy")
} catch (e) {
copysuccess = false
}
return copysuccess
}