判断是否为移动端
// 利用是否拥有移动端事件来判断 得到一个布尔值
'ontouchend' in document
禁止复制网页内容
<script type="text/javascript">
// oncontextmenu 事件在元素中用户右击鼠标时触发并打开上下文菜单
document.oncontextmenu=new Function("event.returnValue=false");
// onselectstart几乎可以用于所有对象,其触发时间为目标对象被开始选中时(即选中动作刚开始,尚未实质性被选中)
document.onselectstart=new Function("event.returnValue=false");
</script>
复制大量文字版权附加
$('body').on('copy', function (e) {
if (typeof window.getSelection == 'undefined') {
return;
}
var body_element = document.body,
selection = window.getSelection();
if (('' + selection).length < 30) {
return;
}
var newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
body_element.appendChild(newdiv);
newdiv.appendChild(selection.getRangeAt(0).cloneContents());
if (selection.getRangeAt(0).commonAncestorContainer.nodeName == 'PRE') {
newdiv.innerHTML = '<pre>' + newdiv.innerHTML + '</pre>';
};
newdiv.innerHTML += '<br />著作权归作者所有。<br />商业转载请联系作者获得授权,非商业转载请注明出处。<br />原文: <a href="' + location.href + '">' + location.href + '</a> © ntnyq.com';
selection.selectAllChildren(newdiv);
window.setTimeout(function () {
body_element.removeChild(newdiv);
}, 200);
});
创建可下载文件
function createFileDownload (filename, content) {
var aLink = document.createElement('a'),
blob = new Blob([content], { type: 'url' }),
event = new Event('click');
aLink.download = filename;
aLink.href = URL.createObjectURL(blob);
aLink.click();
URL.revokeObjectURL(blob); // 回收内存
}