单击后,我的添加到书签javascript将不会加载

时间:2012-07-19 16:31:52

标签: javascript html css

我一直想弄清楚为什么点击后我的加入书签javascript不会加载。我认为它与被定位的div有关:绝对就像当我删除所有我的div并且只是它有效的链接。我已经包含了我的CSS,HTML和JavaScript。

html {
   width:100%; 
   height:100%; 
}

body {
    background: #403F3D url(images/background.png) top center fixed;
    margin:0;
    padding:0;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    color:#444;
    font-size:12px;
    line-height:1.9em;
    text-align: center;
}

* {
    margin:0;
    padding:0;
}

#container {
    position:absolute;
    top: -10px;
    left: 50%;
    margin-left: -447.5px;
    text-align:left;
}

#bookmark {
    position:absolute;
    left:222px;
    top:343px;
    width:282px;
    height:46px;
}

<div id="container"> 
<div id="bookmark">
<a href="javascript:bookmarksite('WoW Mania - World of Warcraft Gameplay Guide', 'http://www.wowmania.net')">
<img src="images/bookmark.png"  class="domroll images/active-bookmark.png"></a>
</div> 
</div>

function bookmarksite(title,url){
if (window.sidebar) // firefox
    window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
} 
else if(document.all)// ie
    window.external.AddFavorite(url, title);
}

2 个答案:

答案 0 :(得分:0)

您使用的浏览器是什么?您的链接适用于我的Firefox(V14.01),但不适用于Internet Explorer。 IE总是倾向于挑剔。

检查IE中的开发者工具(按F12),我收到错误:

SCRIPT438: Object doesn't support property or method 'addPanel' 
bookmark.js, line 10 character 2

与您的专栏相关联:

window.sidebar.addPanel(title, url, "");

所以,我认为这是浏览器兼容性的事情。最好检查:

Add to browser favorites/bookmarks from JavaScript but for all browsers (mine doesn't work in Chrome)?

它看起来有点混乱,但答案就在那里。

答案 1 :(得分:0)

试试这个,我从原版修改过的脚本。适用于IE,FF和Opera,而chrome和Safari则提供用户提示。遗憾的是Webkit浏览器不允许它。

BookMarkMe = function () {
    var isIEmac = false; /*@cc_on @if(@_jscript&&!(@_win32||@_win16)&& 
    (@_jscript_version<5.5)) isIEmac=true; @end @*/
    var isMSIE = (-[1,]) ? altIeCheck() : true; //added my own embellishment as the original (-1,]) ? false : true didn't work in ie10
    var cjTitle = document.title;
    var cjHref = location.href;

    function hotKeys() {
        var ua = navigator.userAgent.toLowerCase();
        var str = '';
        var isWebkit = (ua.indexOf('webkit') != - 1);
        var isMac = (ua.indexOf('mac') != - 1);
        var isIe = (ua.indexOf('msie') != - 1);

        if (ua.indexOf('konqueror') != - 1) {
            str = 'CTRL + B'; // Konqueror
        } else if (window.home || isWebkit || isIEmac || isMac) {
            str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D'; // Netscape, Safari, iCab, IE5/Mac
        } else if (isIe){
            str = ('CTRL + D');
        }
        return ((str) ? 'Press ' + str + ' to bookmark this page.' : str);
    }

    function altIeCheck(){
        return (navigator.userAgent.toLowerCase().indexOf('msie') != - 1);
    }

    function isIE8() {
        var rv = -1;
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null) {
                rv = parseFloat(RegExp.$1);
            }
        }
        if (rv > - 1) {
            if (rv >= 8.0) {
                return true;
            }
        }
        return false;
    }

    function addBookmark(a) {
        try {
            if (typeof a == "object" && a.tagName.toLowerCase() == "a") {
                a.style.cursor = 'pointer';
                if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
                    window.sidebar.addPanel(cjTitle, cjHref, ""); // Gecko
                    return false;   
                } else if (isMSIE && typeof window.external == "object") {
                    if (isIE8()) {
                        window.external.AddToFavoritesBar(cjHref, cjTitle); // IE 8                    
                    } else {
                        window.external.AddFavorite(cjHref, cjTitle); // IE <=7
                    }
                    return false;
                } else if (window.opera) {
                    a.href = cjHref;
                    a.title = cjTitle;
                    a.rel = 'sidebar'; // Opera 7+
                    return true;
                } else {
                    alert(hotKeys());
                }
            } else {
                throw "Error occurred.\r\nNote, only A tagname is allowed!";
            }
        } catch (err) {
            alert(err);
        }

    }

    return {
        addBookmark : addBookmark
    }
}();