Firefox and Javascript issue
January 4, 2007 12:02 PM   Subscribe

I'm looking for JavaScript code that successfully updates the href value for the "application/rss+xml" link in a way that enables the Firefox Live Bookmarks functionality to work with a href value assigned at run-time.

So here is example code that works in IE7, but not Firefox 1.5/2.0. The value is changed in the DOM of FF, but clicking the Live Bookmarks icon brings one to non_existant.xml.
<html><head><link rel="alternate" type="application/rss+xml" id="test" title="Test" href="non_existant.xml"></head>
<body>
<script type="text/javascript">
  document.getElementById('test').setAttribute('href','test.xml');
  //or alternatively
  document.getElementById('test').href= "test.xml";
</script></body></html>
posted by McGuillicuddy to Technology (6 answers total) 3 users marked this as a favorite
 
Best answer: It looks like Firefox only listens for the DOMLinkAdded event. For example, the following code adds an additional live bookmark link.
  var link = document.createElement("link");
  link.setAttribute("rel", "alternate");
  link.setAttribute("type", "application/rss+xml");
  link.setAttribute("title", "some title");
  link.setAttribute("href", "exists.html");
  document.childNodes[0].appendChild(link);
(document.childNodes[0] is the head element)


Using DOM manipulation to replace or remove the existing links has no effect on Firefox's display of the Live Bookmarks. That, as well as
what appears to be the code Firefox uses to handle Live Bookmarks
leads me to believe Firefox ignores link removal completely. This means you can add additional feeds to a page, but you cannot remove existing feeds. If you only need to generate one dynamic feed per page, you can omit your hard coded link tag and use the above code to dynamically generate a link element and add it to the page.
posted by null terminated at 1:56 PM on January 4, 2007


Wrap it up in a function, and call that function on page load (window.onload = myFunction). Maybe the element isn't yet in the DOM when the JS is called.
posted by androse at 1:57 PM on January 4, 2007


Can you just use:
document.write("<link rel=\"alternate\" type=\"application/rss+xml\" id=\"test\" title=\"Test\" href=\"Path_to.xml\">");
It's not pretty, but as long as you aren't updating more than once via, say, AJAX (and I don't think you are..you say at "run-time"), it should work.
posted by niles at 5:42 PM on January 4, 2007


Response by poster: Thanks for the answers so far. Especially thank you null terminated for the effort involved in your answer and for the link to the FF source code for this functionality.

The functionality I'm trying to implement willl be part of an Ajax application, and eventually will be encapsulated as a function that will update the link element without refreshing the entire page. Only 1 feed per page is needed and desired. The example code given was a basic as possible to highlight the "bug".

null terminated's answer worked to add a new feed, however as he notes, not for removing an existing feed link. In my testing, if there is not a hard-coded "application/rss+xml" link element, neither FF nor IE7 display a Live Bookmarks icon despite JavaScript creating a link element. So the code works to add a subsequent feed, but then the user would have to choose between a valid feed and the original invalid feed. In my testing FF also didn't properly use the Title of the JS generated link element for the feed title.

So I'm still looking for a solution, but it looks like it is a FF bug or improvement request.
posted by McGuillicuddy at 6:38 AM on January 5, 2007


This code seems to work fine with my version of Firefox (2.0.0.1). It initially displays no feeds and then displays an additional feed each time you click on the button.

--------------------------------
<html>
<head>
<script type="text/javascript">
function createLink(){
var head = document.getElementsByTagName('head').item(0);
var link = document.createElement("link");
link.setAttribute("rel", "alternate");
link.setAttribute("type", "application/rss+xml");
link.setAttribute("title", "test 2");
link.setAttribute("href", "exists.html");
head.appendChild(link);
}

</script>
</head>
<body>
</body>

<input type="button" value="hey" onclick="createLink();">
</html>

--------------------------------

Since Explorer lets you create a Live Bookmark link and then change it and Firefox lets you add a link after the page has loaded, you could create a hard coded link and change the href attribute for Explorer users and create a new link for Firefox users. It's a hack, but it should work.
posted by null terminated at 10:01 AM on January 5, 2007


Response by poster: Thank you, null terminated. You are correct. It also works in FF 1.5.8+. And Safari 2.0 (419.3). But it does not use the title attribute appropriately as the title for the feed bookmark (1.5 functionality). And it does not work in IE7, although either method in my original post does.

I tested quickly yesterday, and in my post above I mistakenly combined the IE7 functionality (it works for adding link elements, but a hard coded link feed has to exist too) and the broken FF 1.5 functionality into "doesn't work". I should have tested better before writing it up. With a cross-browser hack, it will work.
posted by McGuillicuddy at 12:33 PM on January 5, 2007


« Older Electronics 101...or lower   |   Quicktime: use streaming or http? Newer »
This thread is closed to new comments.