views:

145

answers:

3

Is it possible to dynamically the URL of a feed that has been instantiated in the header of a page via JavaScript?

For example, is it possible to update the URL of the rss feed in the following snippet:

<html>
  <head>
    <link rel="alternate" title="Feed" href="/rss.feed" type="application/rss+xml">
  </head>
  <body>
    ...
  </body>
</html>
+1  A: 

I guess you could do this:

<script type="text/javascript">
linkCol = document.getElementsByTagName("link");
for(i = 0; i < linkCol.length; i++) {
    if(linkCol[i].title == "Feed") {
        linkCol[i].href = "NEW HREF";
    }
}
</script>
Salty
+1  A: 

Try the following:

document.getElementsByTagName("link")[0].href = "http://example.com/";

If it’s not the first and only link element, you have to look for it first, e. g.:

var linkElems = document.getElementsByTagName("link");
for (var i in linkElems) {
    if (linkElems[i].href === "/rss.feed") {
        linkElems[i].href = "http://example.com/";
        break;
    }
}
Gumbo
+1  A: 

I'm pretty sure that most browsers will not see the change. But I'd love to be proven wrong.

Allain Lalonde