Here's an example of how you can use a cookie to bookmark a page in the user's browser
Mark E.
<script>
function setBookmark() {
// Set a cookie with the current page URL
document.cookie = "bookmark=" + encodeURIComponent(window.location.href);
alert("Page bookmarked!");
}
function getBookmark() {
// Get the value of the bookmark cookie
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i].trim();
if (cookie.indexOf('bookmark=') == 0) {
return decodeURIComponent(cookie.substring('bookmark='.length));
}
}
return null;
}
// Check if the bookmark cookie is set, and display a message to the user
var bookmark = getBookmark();
if (bookmark) {
alert("You have bookmarked this page: " + bookmark);
}
</script>
<a href="#" onclick="setBookmark()">Bookmark this page</a>
In this example, we define two functions: setBookmark()
and getBookmark()
. setBookmark()
sets a cookie with the current page URL when called, and getBookmark()
retrieves the URL from the cookie.
We then create a link that calls the setBookmark()
function when clicked. When the link is clicked, the cookie is set and an alert is displayed to confirm that the page has been bookmarked.
Finally, we check if the bookmark cookie is set when the page loads, and display a message to the user if it is.
Note that this example uses a simple approach to store the bookmark URL in a cookie, which may not be suitable for all use cases. You may want to consider other approaches, such as using browser storage or server-side storage, depending on your needs.