How to create a custom Bookmark Button on Website ?
SEO Help and Tips
How to create a custom Bookmark Button on Website ?
If you want to provide a convenient way for users to bookmark your webpage using a bookmark button, you can use JavaScript to create a bookmark link. Here's an example of how you can create a bookmark button that, when clicked, adds the current page to the user's bookmarks:
Here is the code for create Bookmark Button:
html:
<head>
<title>Bookmark Button Example</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is some content on my webpage.</p>
<button id="bookmarkButton">Bookmark This Page</button>
<script>
document.getElementById("bookmarkButton").addEventListener("click", function() {
// Get the current page's URL
var currentPageUrl = window.location.href;
// Get the current page's title
var currentPageTitle = document.title;
// Create a new bookmark link element
var bookmarkLink = document.createElement("a");
bookmarkLink.href = currentPageUrl;
bookmarkLink.title = currentPageTitle;
bookmarkLink.textContent = currentPageTitle;
// Append the bookmark link to the browser's bookmarks or favorites
if (window.sidebar && window.sidebar.addPanel) {
// For Firefox
window.sidebar.addPanel(currentPageTitle, currentPageUrl, "");
} else if (window.external && ('AddFavorite' in window.external)) {
// For Internet Explorer
window.external.AddFavorite(currentPageUrl, currentPageTitle);
} else if (window.opera && window.print) {
// For Opera
bookmarkLink.rel = "sidebar";
bookmarkLink.href = currentPageUrl;
bookmarkLink.title = currentPageTitle;
bookmarkLink.textContent = currentPageTitle;
bookmarkLink.click();
} else {
// For other browsers or unsupported methods
alert("Your browser doesn't support automatic bookmarking. Please press Ctrl+D (Cmd+D on Mac) to bookmark this page.");
}
});
</script>
</body>
Python:
import urllib.parse
def generate_bookmarklet(url, title):
bookmarklet_template = """
javascript:(function(){var%20url=%22{}%22;var%20title=%22{}%22;if(document.all)%7Bwindow.external.AddFavorite(url,title);%7Delse%20if(window.sidebar)%7Bwindow.sidebar.addPanel(title,url,%22%22);%7Delse%7Balert(%22Your%20browser%20doesn't%20support%20automatic%20bookmarking.%20Please%20press%20Ctrl+D%20(Cmd+D%20on%20Mac)%20to%20bookmark%20this%20page.%22);%7D})();
"""
return bookmarklet_template.format(urllib.parse.quote(url), urllib.parse.quote(title))
# Example usage
url = "https://www.example.com"
title = "Example Website"
bookmarklet = generate_bookmarklet(url, title)
print(bookmarklet)
Note: Keep in mind that browser support for adding bookmarks programmatically can vary, and some modern browsers might not support this feature due to security concerns. It's always best to provide users with clear instructions on how to bookmark a page manually using their browser's built-in features (e.g., pressing Ctrl+D or Cmd+D).
Comments
Post a Comment
Thanks for your Comments.