PDA

View Full Version : Why does this script work in Firefox but not work in IE?



DIYGUY
03-19-2009, 05:44 PM
As a parallel thread to my earlier one about bookmarking URLs, this post is the source of the trouble that I am trying to solve with my previous post. If I cannot find any help with that post, perhaps someone can help me solve this issue?

I inherited the following script from an app and it works just fine in FireFox, but not in IE. If you have any insights to share on why this is the case and what I could do to make it work in both realms please share them.

Here is the Jscript that works fine in FireFox:

function addbookmark(){
if (document.all) {
window.external.AddFavorite(window.location.href,document.title);
} else {
alert("Use CTRL-D to add a bookmark in your Browswer.");
}}
function land(ref, target)
{
lowtarget=target.toLowerCase();
if (lowtarget=="_self") {window.location=loc;}
else {if (lowtarget=="_top") {top.location=loc;}
else {if (lowtarget=="_blank") {window.open(loc);}
else {if (lowtarget=="_parent") {parent.location=loc;}
else {parent.frames[target].location=loc;};
}}}
}


This is how it is called from the XHTML:
<a href="javascript:addbookmark()"><img src="images/bookmark.gif"" /></a>

This is in the head section of the pages that use the script:
<script language="JavaScript" type="text/javascript" src="Jscript/bookmark.js"></script>

The only thing this little script does is create a popup window with the alert text in it. The message that IE gives the viewer is "Error on page." The popup blocker is turned off.

All comments and thoughts are welcome!

jscheuer1
03-20-2009, 09:15 AM
The only code being used is:


function addbookmark(){
if (document.all) {
window.external.AddFavorite(window.location.href,document.title);
} else {
alert("Use CTRL-D to add a bookmark in your Browswer.");
}}

IE and Opera both support document.all - so will attempt:


window.external.AddFavorite(window.location.href,document.title);

I believe that will give an error in IE if the page is local. Perhaps even with a live page. In Opera, there is no window.external.AddFavorite, so there will just be an error, but Opera is usually silent when it encounters script errors.

All others presumably will do:


alert("Use CTRL-D to add a bookmark in your Browswer.");

If that's all that you want to have happen anyway, just change the function to:


function addbookmark(){
alert("Use CTRL-D to add a bookmark in your Browswer.");
}

DIYGUY
03-20-2009, 12:14 PM
Thank you John - your suggestion has solved my problem beautifully (at least on first pass).