Add current page to browser bookmarks

Asked

Viewed 371 times

4

The code below is working, but the technique is old and probably outdated compared to current versions of browsers:

function addFavorite( a, b ) {
  title = document.title;
  url = document.location;
  try {
    // Internet Explorer
    window.external.AddFavorite( url, title );
  }
  catch (e) {
    try {
      // Mozilla
      window.sidebar.addPanel( title, url, "" );
    }
    catch (e) {
      // Opera
      if( typeof( opera ) == "object" ) {
        a.rel = "sidebar";
        a.title = title;
        a.url = url;
        return true;
      }
      else {
        // Unknown
        alert( b );
      }
    }
  }
  return false;
}

Utilizing:

<a href="#" title="" onclick="addFavorite(this, 'Pressione Ctrl-D para adicionar a página aos seus favoritos');return false;">
    Adicionar aos favoritos
</a>

Question

The process in its current form is running efficiently or it can be simplified and updated to match the specifications of current versions of browsers?

1 answer

1

Virtually all functions that you used still work, but does not work in all browsers and there is no solution for this, for example Chromium browsers (Google Chrome, Opera, Comodo Dragon, etc) block javascript access to this method.

You can still use it and it will work on a portion of browsers, but it is likely that actually these functions are "disabled" (discontinued).

The likely disuse of these methods, the idea is to make the user himself decide whether or not to add the page to the favorites.

Discontinued methods

Apparently the only one rel="sidebar" was the only one not discontinued, the reason is that he works differently from other methods, as per the HTML Standard the attribute shall indicate that the referenced document (if obtained) shall be intended to display in the contextMenu the address indicated instead of the current page, if in this menu you have the option to save to bookmarks, this does not mean that all browsers follow this rule (in Opera and Chrome it seems to work). Apparently it should also have effect with <link>, like for example, if you add on the page http://exemplo/blog the following tag:

<link rel="sidebar" href="http://examplo">

The moment the user clicks Ctrl+D instead of using the page http://exemplo/blog will use the page http://exemplo in the bookmaker (I really couldn’t test and it seems to me that it is the same situation of the context menu, it may even be a "standard", but it is not fully respected).

Of course you can still use the obsolete codes (combined with the rel="sidebar"), for example if you want to maintain support for older browsers.

Regarding your code (since you marked the question as ), I would just avoid the try/catch, maybe use if is a better way and would use something less "obstructive" than the attribute onclick="...":

function addFavorite(target) {
    var title = document.title;
    var url   = document.location;

    if (window.external && window.external.AddFavorite) {
        target.onclick = function() {
            window.external.AddFavorite( url, title );
            return false;
        };
    } else if (window.sidebar && window.sidebar.addPanel) {
        target.onclick = function() {
            window.sidebar.addPanel( title, url, "" );
            return false;
        };
    } else {
        target.onclick = function() {
            alert("Aperte Ctrl+D para salvar nos favoritos");
            return false;
        };
    }
}

//Use no onload ou no jquery.ready
addFavorite($("a[rel=sidebar]").get(0));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<a href="http://exemplo/teste" rel="sidebar" title="Site">Exemplo</a>

Note that today Opera uses Chrome technology (Webkit’s Blink Fork) and may not work on Opera either, as it is now Googlechrome’s "family".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.