Forum Moderators: open

Message Too Old, No Replies

How to do correct code for the button Bookmark It!

         

toplisek

8:05 am on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have message when validating like:

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]

rocknbil

12:17 am on Nov 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would do something like this. S.E.'s don't need to follow that link anyway.

<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]

toplisek

11:28 am on Jan 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, this script works without error when validating links. Issue is that it is some posponed time when showing text like Add to favourites.

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]