Letting the visitor to add the current visted page to his list of favorites websites or set it as home page might seem an easy task at first sight but let me assure you it’s not.
Let’s see why is that.
In the first place we’ll need two links:
-
one to add the page to favorites
-
and the other one to set the page as home page,
so we need a reference to their ID’s:
// Get a global reference to the objects needed in the script
var favLink = document.getElementById(“AddPageToFavorites_Link”);
var homePageLink = document.getElementById(“SetAsHomePage_Link”);
Next, we’ll create an Object, called “UserSpecifics” to hold the functions we need:
var UserSpecific =
{
……
};
The first function will check the existance of those two links and if they’re found then the “click” event will be attached to them.
init: function()
{
// Check the existance of the required objects
if (!favLink || !homePageLink) { return null; }
else {
Core.addEventListener(favLink, “click”, UserSpecific.favLinkClicked);
Core.addEventListener(homePageLink, “click”, UserSpecific.setAsHPLinkClicked);
}
},
The following three functions will use to accomplish this task:
1. the first one will return the location of the current visited page:
// <summary>
// <description>Gets the current page location.</description>
// </summary>
getCurrentLocation: function() {
return window.location.href;
},
2. the second one will add the page to favorites:
// <summary>
// <description>Adds a page to favorites.</description>
// </summary>
addPageToFavorites: function() {
var title = document.title, location = UserSpecific.getCurrentLocation();
if (document.all) {
window.external.AddFavorite(location, title);
}
else if (window.sidebar) {
window.sidebar.addPanel(title, location, “”);
}
},
3. the third one will set the page as home page:
// <summary>
// <description>Sets as Home Page the current visited page.</description>
// </summary>
setAsHomePage: function() {
var location = UserSpecific.getCurrentLocation();
homePageLink.style.behavior = “url(#default#homepage)”;
homePageLink.setHomePage(location);
},
Next, we’ll handle the “click” event handlers we’ve set earlier:
1. for the “favLink” link:
favLinkClicked: function(event)
{
UserSpecific.addPageToFavorites();
Core.preventDefault(event);
},
2. for the “homePageLink” link:
setAsHPLinkClicked: function(event)
{
UserSpecific.setAsHomePage();
Core.preventDefault(event);
}
Initializing our Object:
Core.start(UserSpecific);
So far so good, now let’s see the bad part:
You’ll notice this script will work as expected in Internet Explorer browser but not in Safari or Opera. In Mozilla-based browsers you can’t use the set as home page function, only the add to favorites one. I checked the Mozilla forums to see if they found a solution to this problem but couldn’t find one, so it seems this is an internal Firefox, Netscape… bug.
Let’s hope the next version of Firefox will fix this problem.
**Testing:
If you’d like to test this script, follow this link: AddPageToFavorites.
**Note
This script uses the Core Javascript library. You can download it by following this link.
Popularity: 11% [?]




SEO MegaCorp news blog is dedicated to provide tips, tricks and latest news around the industry that one might miss. We're a SEO company which is based in India, which is dedicated only to serve the clients with thier full satisfaction...