{"id":1517,"date":"2013-04-09T22:17:53","date_gmt":"2013-04-09T22:17:53","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=1517"},"modified":"2013-04-09T22:17:53","modified_gmt":"2013-04-09T22:17:53","slug":"add-favorites-javascrip","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/add-favorites-javascrip\/","title":{"rendered":"Add to Favorites with JavaScript &#8211; the smarter way"},"content":{"rendered":"<p>Let&#39;s get it out of the way now. Yes, the &quot;Add to Favorites&quot; button is pretty old-school. The modern web user is savvy enough to be able to quickly identify and use their web browsers bookmarking feature without much struggle.<\/p>\n<p>But how many of us simply forget to bookmark a page or tool that we&#39;d like to reference often? Having a small, intelligent, unobtrusive reminder to bookmark the page isn&#39;t going to force any visitors into a steaming fit of rage.<\/p>\n<p>This is an okay practice, if used sparingly and appropriately:<\/p>\n<ol>\n<li>Don&#39;t place an &quot;Add to Favorites&quot; button on your home page, it should be reserved for deep links that the user might have difficulty locating again.<\/li>\n<li>Only display the link if JavaScript is available (since JavaScript is required to add the favorite to the browser).<\/li>\n<li>If a user has added the page to their bookmarks or favorites, don&#39;t display the link or icon to do so.<\/li>\n<\/ol>\n<p>The first one is simple, just don&#39;t do it! The second one makes us think about implementation. For obvious reasons, the browser does not expose the current user&#39;s bookmarked pages in a request header or to JavaScript. So, how can we find out if the user has added our page as a favorite.<\/p>\n<p>The short answer is, we can&#39;t. But, we can get close. By leveraging <a href=\"\/blog\/html-5-storage-better-cookies\/\" target=\"_blank\">HTML 5 Storage<\/a>, we can track when a user has clicked the &quot;Add to Favorites&quot; button in a local storage variable. With each page load, we&#39;ll check this variable to see if it contains the current page. If it does, we do not display the &quot;Add to Favorites&quot; button.<\/p>\n<p>You&#39;ll notice in the code below that the bookmark &quot;button&quot; is added via JavaScript to the end of the &lt;h1&gt; tag. You can, of course, append it wherever you like. The purpose of this line is to demonstrate item #2 in the list above. We don&#39;t want this button to display if JavaScript is not available.<br \/>\n\t&nbsp;<\/p>\n<h3>jQuery Add Bookmark Code<\/h3>\n<p><code>var page = location.href.replace(location.hostname, &#39;&#39;).replace(&#39;http:\/\/&#39;, &#39;&#39;).replace(&#39;https:\/\/&#39;, &#39;&#39;);<br \/>\n\tif (typeof Storage !== &#39;undefined&#39;) {<br \/>\n\t&nbsp; if (window.localStorage.getItem(&#39;bmarkAttempt&#39;)) {<br \/>\n\t&nbsp; &nbsp; if (window.localStorage.getItem(&#39;bmarkAttempt&#39;).indexOf(page) !== -1) {<br \/>\n\t&nbsp; &nbsp; &nbsp; return false;<br \/>\n\t&nbsp; &nbsp; }<br \/>\n\t&nbsp; }<br \/>\n\t}<\/p>\n<p>\t$(&#39;h1&#39;).append(&#39;&lt;a id=&quot;bookmark&quot;&gt;&lt;img src=&quot;\/mybookmarkimage.jpg&quot; alt=&quot;Add to Fav\\&#39;s&quot; \/&gt;&lt;\/a&gt;&#39;);<\/p>\n<p>\t$(&quot;#bookmark&quot;).click(function() {<br \/>\n\t&nbsp; BookmarkApp.addBookmark(this);<\/p>\n<p>\t&nbsp; if (typeof Storage !== &#39;undefined&#39;) {<br \/>\n\t&nbsp; &nbsp; var bmarkAttempt = &#39;&#39;;<\/p>\n<p>\t&nbsp; &nbsp; if (window.localStorage.getItem(&#39;bmarkAttempt&#39;)) {<br \/>\n\t&nbsp; &nbsp; &nbsp; bmarkAttempt = window.localStorage.getItem(&#39;bmarkAttempt&#39;);<br \/>\n\t&nbsp; &nbsp; }<\/p>\n<p>\t&nbsp; &nbsp; window.localStorage.setItem(&#39;bmarkAttempt&#39;, bmarkAttempt + &#39;,&#39; + page);<br \/>\n\t&nbsp; }<\/p>\n<p>\t&nbsp; $(this).remove();<br \/>\n\t});<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>You&#39;ll notice the use of a function called BookmarkApp. You can copy\/paste the code for this function from the creator&#39;s website at:&nbsp;<a href=\"http:\/\/eureka.ykyuen.info\/2011\/07\/21\/javascript-add-to-bookmark\">http:\/\/eureka.ykyuen.info\/2011\/07\/21\/javascript-add-to-bookmark<\/a><\/p>\n<p>However, you&#39;ll need to change one line of the code to function with IE.<\/p>\n<p>Change:<\/p>\n<p><code>var isMSIE = (-[1,]) ? false : true;<\/code><\/p>\n<p>to<\/p>\n<p><code>var isMSIE=(navigator.appName==&quot;Microsoft Internet Explorer&quot;)?true:false;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#39;s get it out of the way now. Yes, the &quot;Add to Favorites&quot; button is pretty old-school. The modern web user is savvy enough to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1522,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[5],"tags":[248],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1517"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=1517"}],"version-history":[{"count":3,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions"}],"predecessor-version":[{"id":1523,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/1517\/revisions\/1523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media\/1522"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=1517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=1517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=1517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}