Forum Moderators: open

Message Too Old, No Replies

Returning from a function

IE doesn't seem to like this

         

rocknbil

5:16 pm on Jul 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got one for you! :-)

A legacy project had an inline "bookmarking" script (I know, I know . . . .) I initially removed the code from the page and put it in an external file, like so:

var bookmark = getBookmark('full_url_to_page','Page Title For the Bookmark');
if (bookmark) { document.write(bookmark); }

where getBookmark is the standard IE/Moz switch:


function getBookmark(url,who) {
var ver = navigator.appName;
var num = parseInt(navigator.appVersion);
var msg;
if ((ver == "Microsoft Internet Explorer")&&(num >= 4)) {
msg = '<a href="#" onClick="window.external.AddFavorite('+url+','+who+')'+
' return false;">Bookmark<\/a>';
}
else { msg = '<strong>Bookmark \- (Ctrl+D)</strong>'; }
return msg;
}

IE choked on it, but not on the function - on this line:
var bookmark = getBookmark('full_url_to_page','Page Title For the Bookmark');

Says ") expected."

I've since put it back inline, and it works fine. It doesn't error if I do this,

var bookmark = getBookmark('full_url_to_page');

But that's also worthless. Escaping special characters in the parameters don't have any effect.

What's the problem here?

Fotiman

2:51 pm on Jul 7, 2008 (gmt 0)

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



Not sure if this would cause the problem you're seeing, but you're missing a semicolon before the "return false;" statement in your msg var (for IE).

rocknbil

4:48 pm on Jul 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Typo here, I added that to prevent the wide lines on this board. No it's not in the external script, it's on the line in the HTML, for example, if my function is empty

function getBookmark (url,who) {
}

It still errors on this line in the HTML

var bookmark = getBookmark('full_url_to_page','Page Title For the Bookmark');

I've played with escaping values in the parameters, blanked them out, for whatever reason, it seems to be the two parameters causing an error in IE.

Fotiman

7:57 pm on Jul 8, 2008 (gmt 0)

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



The problem is that you are passing in a string to getBookmark, but in your method you are writing those strings out without surrounding them in quotes. Here's an updated version that works:


function getBookmark(url,who) {
var ver = navigator.appName;
var num = parseInt(navigator.appVersion);
var msg;
if ((ver == "Microsoft Internet Explorer") && (num >= 4)) {
msg = "<a href='#' onClick='window.external.AddFavorite(\"" + url + "\",\"" + who + "\");return false;'>Bookmark<\/a>";
}
else { msg = '<strong>Bookmark \- (Ctrl+D)</strong>'; }
return msg;
}
var bookmark = getBookmark('full_url_to_page','Page Title For the Bookmark');
if (bookmark) {
document.write( bookmark );
}

rocknbil

1:43 am on Jul 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[enlarges forehead-shaped dent in desk . . . .]

Quoting argh . . . . addFavorite and title must be quoted argh . . . . once again, "U Rool", thank you. :-) I swapped them around so my params get single quotes and maintain double quotes in HTML, but yes . . . that was it.