JavaScript Popup Copy Code
Letakkan kode ini di atas </body>
:
<script>
//<![CDATA[
function copy_code(id) {
var ref = document.getElementById(id),
code = ref.getElementsByTagName('code')[0].innerHTML,
w_w = window.innerWidth,
w_h = window.innerHeight,
win = window.open('', '', 'left=' + ((w_w/2)-250) + ',top=' + ((w_h/2)-150) + ',width=500,height=300,scrollbars=0');
win.document.write('<!DOCTYPE html><html><head><title>Source Code</title><style>*{margin:0;padding:0}body{padding:10px;text-aign:center}textarea{display:block;width:98%;height:270px;padding:1px 1px;margin:0 auto;text-align:left;overflow:auto}</style></head><body><textarea>' + code.replace(/<(.*?)>/g, "") + '</textarea><scr' + 'ipt>var a=document.getElementsByTagName(\'textarea\')[0];a.focus();a.select();</scr' + 'ipt></body></html>');
}
function add_copy_button() {
var pre = document.getElementsByTagName('pre');
for (var i = 0; i < pre.length; i++) {
pre[i].id = 'code-' + i;
pre[i].className += ' quick-copy';
pre[i].innerHTML += '<a class="c_b" href="javascript:copy_code(\'code-' + i + '\');">Copy</a>';
}
} add_copy_button();
//]]>
</script>
Lalu tambahkan kode CSS ini di atas ]]></b:skin>
atau </style>
:
pre {position:relative}
pre .c_b {
display:block;
position:absolute;
top:0;
right:0;
padding:2px 5px;
}
Markup HTML
<pre><code> ... </code></pre>
Demo:
Labels: Blogger, JavaScript, Potongan
13 Comments:
Terima Kasih mas..
:-bd
By Unknown, at Saturday, November 17, 2012 at 10:49:00 PM GMT+7
Good \o/
By Unknown, at Saturday, November 17, 2012 at 10:55:00 PM GMT+7
cooool \o/ :yaya:
By Unknown, at Sunday, November 18, 2012 at 9:00:00 AM GMT+7
kok kek kayak ini ya
http://hompimpaalaihumgambreng.blogspot.com/2012/05/view-plain-code.html
By Beben Koben, at Monday, November 19, 2012 at 6:58:00 PM GMT+7
Yang ini tanpa JQuery, versi JavaScript mentah.
By Taufik Nurrohman, at Monday, November 19, 2012 at 7:48:00 PM GMT+7
ehm...telimikiciw :)
By uki, at Saturday, November 24, 2012 at 2:25:00 PM GMT+7
Mas, ini kan untuk <pre></pre> , kalo untuk <blockquote></blockquote> gimana solusinya ya mas??? :)
By Anonymous, at Wednesday, December 19, 2012 at 10:00:00 PM GMT+7
Ganti pre dengan blockquote:
var pre = document.getElementsByTagName('blockquote');
Format kode harus tetap menggunakan elemen <code> di dalamnya:
<blockquote>
<code> ... </code>
</blockquote>
By Taufik Nurrohman, at Thursday, December 20, 2012 at 6:14:00 PM GMT+7
mau nanya nih bang ..
ane udah buat pre-tag vanilla ,
nah untuk buat tombol copynya ke dalam pre-tag vanilla gmna ya ??
By Unknown, at Saturday, May 4, 2013 at 3:41:00 AM GMT+7
Ikuti saja perintahnya, nanti tombolnya akan muncul sendiri. Atau kalau tidak muncul, mungkin tombolnya terselip di belakang. Jadi nilai z-index tombol harus ditambahkan dan diperbesar:
pre .c_b {
display:block;
position:absolute;
top:0;
right:0;
padding:2px 5px;
z-index:99;
}
By Taufik Nurrohman, at Saturday, May 4, 2013 at 6:15:00 AM GMT+7
masih bingung mas save dulu mas yah
By Anonymous, at Friday, May 31, 2013 at 8:38:00 AM GMT+7
mas buatin donk tutorial biar open source blog kita tidak bisa di baca seperti situsnya mas ini.
By Unknown, at Friday, May 29, 2015 at 9:26:00 PM GMT+7
Maksudnya CSS dan JavaScript eksternal?
→ /2015/01/perkenalan-html.html
By Taufik Nurrohman, at Sunday, May 31, 2015 at 3:33:00 PM GMT+7
Post a Comment
<< Home