Home > Back-end >  Is there a way to have a bookmark be created upon clicking a button/link?
Is there a way to have a bookmark be created upon clicking a button/link?

Time:10-04

I'm trying to figure out if it's possible to make it so that if you click a link or button, a bookmark will be created. Anything that comes up on Google has to do with ebooks, and I can't seem to find anything. If anyone has the answer, it would be a huge help!

CodePudding user response:

function addFavorites(a) {
    pageTitle = document.title;
    pageURL = document.location;
    try {
        // Internet Explorer solution
        eval("window.external.AddFa-vorite(pageURL, pageTitle)".replace(/-/g, ''));
    } catch (e) {
        try {
            // Mozilla Firefox solution
            window.sidebar.addPanel(pageTitle, pageURL, "");
        } catch (e) {
            // Opera solution
            if (typeof(opera) == "object") {
                a.rel = "sidebar";
                a.title = pageTitle;
                a.url = pageURL;
                return true;
            } else {
                // The rest browsers (i.e Chrome, Safari)
                alert('Press '   (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Cmd' : 'Ctrl')   ' D to bookmark this page.');
            }
        }
    }
    return false;
}
<a href="javascript:void(0)" onClick="return addFavorites(this);">Add to Favorites</a>

Source: https://rudrastyh.com/javascript/favorite-button.html

Be aware of eval()

eval() may be a dangerous function under certain circumstances, which executes the code it's passed with the privileges of the caller.

Get advised by Mozilla.org and use with caution

CodePudding user response:

No, there's no stright forward way for that.

Opera, Google Chrome and Safari do not provide a way to add new elements to the Favorites (Bookmarks) from JavaScript.

There was an API for that however, which is the Window.external API, and I believe it used to work as window.external.AddFavorite() in Internet Explorer, and window.sidebar.addPanel() in Firefox (obsolete, deprecated, i.e. dead, see for the API).

Not sure this info would help, but you might have to create a browser extension for that, then connect your button with your extension, this way you can ensure its stability.

My suggestion is to simply ask the users to bookmark the webpage they're in if that would help to solve the problem you're trying to solve (not sure what you're trying to create).

  • Related