Forum Moderators: open
MESSAGE:
What to do: You must change this link: people using a browser without JavaScript support will not be able to follow this link
CODE:
Code that I use is the following:
<a href="javascript:bookmarksite('Your Website', 'http://www.example.com')" class="top">Add To Favorites</a>
JAVASCRIPT CODE:
function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}
[edited by: Fotiman at 5:45 pm (utc) on Jan. 8, 2009]
[edit reason] examplified domain [/edit]
<div id="where_my_link_goes"></div>
<script type="text/javascript">
function bookmarksite(title, url){
if (document.all) {
window.external.AddFavorite(url, title);
}
else if (window.sidebar) {
window.sidebar.addPanel(title, url, "");
}
return false;
}
function writeLink() {
if (document.getElementById('where_my_link_goes')) {
var lnk = '<a href="#" onClick="return bookmarksite(\'Your Website\','+
'\'http://www.example.com\')"' +
' class="top">Add To Favorites</a>';
document.getElementById('where_my_link_goes').innerHTML=lnk;
}
}
window.onload=function() { writeLink(); }
</script>
So when the page loads, you have an empty div (or p, or whatever.) On load, if Javascript is enabled, it outputs the link.
Ideally, you would take a different action (if/else if) for browsers that don't support the bookmark methods.
Note the addition of return in the link, and return false from the function. This stops the browser from doing it's normal action, navigating on click of the link. This allows you to get rid of href="javascript...." which is an invalid URL anyway.
[edited by: Fotiman at 5:46 pm (utc) on Jan. 8, 2009]
[edit reason] examplified domain [/edit]
But there are two issues:
It seems as there will be first shown URL in DIV element and than it shows text like <div id="where_my_link_goes">http://www.example.com</div>
If I remove URL like
id="where_my_link_goes"></div>
It will report validating error in this script.
[edited by: Fotiman at 5:47 pm (utc) on Jan. 8, 2009]
[edit reason] examplified domain [/edit]