Johnny Knoxville said:
I've added a favicon to my site (
http://lazyape.filetap.com/) which
works fine if you add the site to favourites the normal way, but I
have some JavaScript code on a couple of pages with a link, which
when you click it bookmarks the site (much easier). The favicon is
never saved if the site is bookmarked this way. Does anyone have any
ideas how to fix this??
Attempting to reproduce normal browser behaviour within a web page falls
into the category of "reinventing the wheal" and even expertly
implemented the result is often square.
This is the code:
<script language="JavaScript">
The language attribute is deprecated in HTML 4+ while the type attribute
is required.
This wrapping script element contents in HTML comments to "hide them
from older browsers" is no longer needed as the "older browsers" that it
defended against have long since gone out of use.
if ((navigator.appName == "Microsoft Internet Explorer") &&
(parseInt(navigator.appVersion) >= 4)) {
Browser detecting based on the properties of the navigator object is
utterly meaningless. The code above might send at least the vast
majority of IE browsers into this branch of the code but it will also
pass a significant number of other browsers spoofing IE but still
incapable of the task the resulting HTML will imply is possible.
Examining the navigator object properties (apart from being next to
useless for the task) is also an unnecessary process as the output HTML
event handler is going to attempt to execute the AddFavorite method of
the external object. So the test logic for this branch should be testing
for the existence of those to objects:-
if(window.external)&(external.addFavorite)){
-which matches the decision to write the A element into the page with
the browsers ability to execute the code it contains with the greatest
precision posible.
var url="
http://www.YOURSITE.com";
var title="YOURSITE";
document.write('<A HREF="java script:window.ext');
The space between "java" and "script" in the above javascript: pseudo
protocol URL will prevent it form working, but it is unwise to use
javascript pseudo protocol URLs in HREF attributes to execute
side-effect code as some browsers (including versions of IE) take
activation of the HREF as navigation and put the current page into a
waiting state in which various resource hungry tasks are shut down
pending the arrival of the result of the navigation (which will never
arrive)).
For an A element the JavaScript code should probably be executed in an
onclick event handler (which has to return false to prevent the
navigation specified in the HREF, which could itself be empty or "#" in
this case as JavaScritp support is implied by the fact that the link has
been written into the page).
However, from the usability point of view, an A element may not be the
best choice for this functionality as users have experienced A elements
as a vehicle for navigation and are used to them performing that role.
An <input type="button"> or <button type="button"> element might be
better suited to the task (though Netscape 4 does not recognise the
latter and would require the former to be placed in a form (not that
important in this branch of the code as only IE supports the external
object)).
document.write('ernal.AddFavorite(url,title);" ');
document.write('onMouseOver=" window.status=');
document.write("'Add YOURSITE to your favorites!'; return true ");
document.write('"onMouseOut=" window.status=');
document.write("' '; return true ");
document.write('"><font size="2" face="Arial, Helvetica, sans-serif">
Font tags are deprecated in HTML 4 (and invalid in strict HTML 4), CSS
styling would be more appropriate in this context (as IE 4+ would have
no problems applying font face and size to this element).
Bookmark our Site!</a></font>');
The sequence of HTML output by this script has gone: opening A tag,
opening FONT tag, closing A tag, closing FONT tag. That is the sort of
nonsense HTML that Microsoft Word outputs and is totally wrong. HTML
elements may not overlap each other in that way they may only contain
each other, FONT should be entirely within A or A should be entirely
within FONT.
That is such a rudimentary mistake that I would take it a grounds to
identify whichever site you found this script on and never visit it
again as its author clearly has not even grasped the fundamentals.
}
else {
var msg = "Bookmark our site!";
if(navigator.appName == "Netscape") msg += " Press CTRL+D keys";
This navigator.appName test is just as meaningless as the first and
there is no relationship between "Netscape" appearing in the appName
property and the use of the key sequence CTRL+D to bookmark a site.
Unfortunately, there is no mechanism by which JavaScript can determine
the bookmaking keyboard shortcut on a browser that does not have an
external.AddFavorite function so writing anything at this point stands a
realistic chance of being misguiding to the reader. It would probably be
best to abandon this - else - branch of the code and rely on the user
having enough nouce to know how to bookmark a page on their own browser
(they don’t all have air between their ears).
document.write(msg);
}
// End -->
</script>
Thanks for your help.
I doubt that this was much help (apart from the advice to give wherever
you fond this script a wide berth in future). If the perceived need to
use this script arises from the use of window.open to attempt to deny
the user the use of their browser menus, or from the use of framesets,
then the best solution would be to stop doing that. If it is just a
bells and whistles extra then trim it down to only act on an
external.AddFavorite supporting browser, or don't bother at all.
Richard.