Forum Moderators: open

Message Too Old, No Replies

calling javascript from a firefox href link

         

abkaiser

6:01 am on Jun 7, 2006 (gmt 0)

10+ Year Member



So I'm pounding my head into the ground trying to fix this. Finally decided to post my problem:

I'm trying to have the user click a link. When that link is clicked, I want to call a javascript function and pass variables.

Here's the header code (I've removed unrelated bits):


<script type="text/javascript">
function saveme(title, url)
{
if (window.sidebar)
{
window.sidebar.addPanel(title, url, "");
}
else
{
return true;
}
}
</script>

As you can see, this code just adds the given variables as a Bookmark in Firefox. Now, here's the body code:


<script type="text/javascript">
if (window.sidebar)
{
var doctitle = document.title;
var doclocation = document.location;
var args = "<a href='javascript:saveme(doctitle, doclocation)'>clickme</a>";
document.write(args);
}
</script>

And it fails with the console message that "saveme is not defined". No matter what I do, I can't fix this. From what I can tell, the function isn't even being called. I *can* get the function to call if I simplify the link above. Specifically, if I take out the href stuff, and just do something like this:


if (window.sidebar)
{
var doctitle = document.title;
var doclocation = document.location;
document.write(saveme(doctitle, doclocation));
}

This leads me to believe this is some goofy syntax formatting requirement on Firefox's end.

Can anyone tell me why my first two code snippets don't work? And how I can get them to work?

Thanks!

Andy

abkaiser

6:03 am on Jun 7, 2006 (gmt 0)

10+ Year Member



...I should add that this code formatting (inserting javascript into a href link) does work fine in IE. But not Firefox.

Andy

encyclo

1:30 pm on Jun 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've just tried your code with nothing else on the page, and it works correctly. Have you cleared your browser cache? Is there anything else interfering with the
saveme
function?

abkaiser

1:52 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



Hmm... I get the same results.

This code is used in a larger page, containing other Javascript in HTML. Posting guides say I can't give links. Let me know if there's a way around that, otherwise let me know what kind of detail you need.

encyclo

2:05 pm on Jun 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Am I right in assuming that you are using a cross-browser bookmarking script which also works for IE? If so, how is the
saveme
function affected?

abkaiser

2:14 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



Initially, I tried to call "saveme" in the same way for all browser types. Here's the code to do that:


<script type="text/javascript">
if (window.external)
{
document.write(' or <a href="javascript:saveme(document.title, document.location)");">add page to Favorites</a>');
}
else if (window.sidebar)
{
document.write(' or <a href="javascript:saveme(document.title, document.location)");">add Bookmark</a>');
}
else if (window.opera && window.print)
{
document.write(' or <a href="javascript:saveme(document.title, document.location)");">add Bookmark</a>');
}
</script>

...And it looks like this, too, works in an "empty" html, but not in my own document.

Does this answer your question?

abkaiser

6:50 pm on Jun 7, 2006 (gmt 0)

10+ Year Member



Solved it. It's because on my production pages, I also had this:

<BASE TARGET="_top">

...And it was overriding the Firefox HREF call. So I put in a target=_self call, like this:

document.write(' or <a target=_self href="javascript:saveme(document.title, document.location)");">add Bookmark</a>');

And, as they say, "voila".

Thanks for getting me there!

Andy