-1

I've been trying to figure out why my add to bookmark javascript won't load when clicked. I think it has something to do with the divs being positioned:absolute as when I remove all my divs and just have the link it works. I have included my css, html and 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);
}
  • What is going on with your img tag? The tag should be closed . But also, I think the "images/active-bookmark.png" isn't a valid classname because of the "/". See if this helps, as you browser may be trying to make up for improper mark-up. – Sablefoste Jul 19 '12 at 16:47
  • The domroll is another javascript.. its an image hover. I have uploaded the site here so you can take a better look? – user1538508 Jul 20 '12 at 07:46
  • http://www.wowmania.net/wowmania2 – user1538508 Jul 20 '12 at 07:46
  • its not finished btw theres no way i'd upload it looking like that i just thought you could get a better idea of what im trying to fix. many thanks!! – user1538508 Jul 20 '12 at 07:48
  • if i remove everything and just have the it works. hence why i thought it was something to do with the divs. its a psd template sliced up. – user1538508 Jul 20 '12 at 07:50

2 Answers2

0

What browser are you using? Your link works in my Firefox (V14.01), but not Internet Explorer. IE always tends to be finicky with JavaScript.

Checking the developer tools in IE (Press F12), I get the error:

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

Which is associated with your line:

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

So, I think this is a browser compatibility thing. Best to check:

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

It looks a little more messy, but the answer is in there.

Community
  • 1
  • 1
Sablefoste
  • 4,032
  • 3
  • 37
  • 58
  • oooh ok. i've tried 4 other bookmark scripts too and none of them work. it's strange as i use the same bookmark script on www.wowmania.net and have no problems in IE. could it be because the script is trying to pop up but its stuck in the div? – user1538508 Jul 20 '12 at 18:19
  • Congratulations! You may want to post your answer to your question here, so others with the same problem can find it easily. – Sablefoste Jul 23 '12 at 01:41
0

Try this, a script I modified from an original. Works in IE, FF and Opera, while chrome and Safari give the user prompts. Sadly Webkit browsers just don't allow it.

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
    }
}();
nospamthanks
  • 112
  • 2
  • 13