Forums / Setup & design / Add to Favourites

Add to Favourites

Author Message

Adam Menary

Thursday 14 June 2007 7:51:01 pm

Is there some way to have an add to favourites link in say an article so that it adds the current page to the web browsers fafourites list?

Joe Kepley

Friday 15 June 2007 10:55:17 am

Here's some javascript that we've used to do this:

A function to support adding bookmarks - you can put this in your header or an included file:

// Released under GNU/GPL License - http://www.gnu.org/copyleft/gpl.htm
// copyright (C) 2005 by Michael Carico - All rights reserved
// website http://www.kabam.net

function displayLink(p_type, p_url, p_title, p_link_text) {
    var agt = navigator.userAgent.toLowerCase();
    var p_here = top.location.href;
    if (p_type == 3) {
        if (agt.indexOf("opera") != -1) {
            document.write("<a href=\"" + p_here + "\" title=\"" + p_title + "\" rel=\"sidebar\">" + p_link_text + "</a>");
        } else {
            document.write("<a href=\"javascript:addBookmark('" + p_title + "',top.location.href);\" title=\"" + p_title + "\" >" + p_link_text + "</a>");
        }
    } else {
        if (agt.indexOf("opera") != -1) {
            document.write("<a href=\"" + p_url + "\" title=\"" + p_title + "\" rel=\"sidebar\">" + p_link_text + "</a>");
        } else {
            document.write("<a href=\"javascript:addBookmark('" + p_title + "','" + p_url + "');\" title=\"" + p_title + "\" >" + p_link_text + "</a>");
        }
    }
}

function addBookmark(title, url) {
    var msg_netscape = "NetScape message";
    var msg_opera = "This function does not work with this version of Opera.  Please bookmark us manually.";
    var msg_other = "Your browser does not support automatic bookmarks.  Please bookmark us manually.";
    var agt = navigator.userAgent.toLowerCase();
    if (agt.indexOf("opera") != -1) {
        if (window.opera && window.print) {
            return true;
        } else {
            alert(msg_other);
        }
    } else if (agt.indexOf("firefox") != -1) {
        window.sidebar.addPanel(title, url, "");
    } else if (agt.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4) {
        window.external.AddFavorite(url, title);
    } else if (agt.indexOf("netscape") != -1) {
        window.sidebar.addPanel(title, url, "");
    } else if (window.sidebar && window.sidebar.addPanel) {
        window.sidebar.addPanel(title, url, "");
    } else {
        alert(msg_other);
    }
}

(Originally from a mambo module)

Then, to create the link (in your article, or template, or elsewhere):

<script language="JavaScript">displayLink('','http://www.example.com','Example Site','Bookmark this page')</script>