Adding A Bookmark Site Link Via A Simple Javascript Function

Have you been to a website that has a link with an anchor text like Bookmark this site, and when you do click it, you prompted to bookmark the site on your PC or Mobile phones?
If you wondering how it was done, worry no more ‘cos am going to share how to implement it on your website.

To do this, Add the below code to the <head> section of your site
[javascript]
<script type=”text/javascript”>

/***********************************************
* Bookmark site script- © The Tech. Diary (www.w3guy.com)
* This notice MUST stay intact for legal use
* Visit us at https://w3guy.com/ for full source code
***********************************************/

/* Modified to support Opera */
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);
}
</script>
[/javascript]
then add
an HTML link with href attribute javascript:bookmarksite(‘Site name here’, ‘http://www.yoursite.com’).

E.g <a href=”javascript:bookmarksite(‘Site name here’, ‘http://www.yoursite.com’)”>Bookmark this site</a>

Remember, if your site’s title contains apostrophes, they need to be backslashed when entered:

<a href="javascript:bookmarksite('Mike\'s Place', 'http://www.google.com')">Bookmark this site!</a>

Just hope you find this useful.

Don’t miss out!
Subscribe to My Newsletter
Invalid email address