加入收藏js代码 兼容所有浏览器
发布时间:2015-11-30, 17:18:31 分类:HTML | 编辑 off 网址 | 辅助
正文 1851字数 163,289阅读
可兼容所有浏览器的加入收藏代码代码,大概原理是这样的我们根据获取用户navigator.userAgent.toLowerCase()
Run code
Cut to clipboard
<script>
function addFavorite2() {
var url = window.location;
var title = document.title;
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("360se") > -1) {
alert("由于360浏览器功能限制,请按 Ctrl+D 手动收藏!");
}
else if (ua.indexOf("msie 8") > -1) {
window.external.AddToFavoritesBar(url, title); //IE8
}
else if (document.all) {
try{
window.external.addFavorite(url, title);
}catch(e){
alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!');
}
}
else if (window.sidebar) {
window.sidebar.addPanel(title, url, "");
}
else {
alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!');
}
}
</script>
<a href=# onclick="javascript:addFavorite2()">加入收藏</a>
Run code
Cut to clipboard
<a href="javascript:void(0);" onclick="addFavorite(window.location,document.title)">收藏我们</a>
Run code
Cut to clipboard
js
function addFavorite() {
var url = window.location;
var title = document.title;
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("360se") > -1) {
alert("由于360浏览器功能限制,请按 Ctrl+D 手动收藏!");
}
else if (ua.indexOf("msie 8") > -1) {
window.external.AddToFavoritesBar(url, title); //IE8
}
else if (document.all) {
try{
window.external.addFavorite(url, title);
}catch(e){
alert('对不起,由于您的浏览器功能限制,请使用菜单栏或按 Ctrl+D 手动收藏!');
}
}
else if (window.sidebar) {
window.sidebar.addPanel(title, url, "");
}
else {
alert('对不起,由于您的浏览器功能限制,请使用菜单栏或按 Ctrl+D 手动收藏!');
}
}
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
暂无评论 »