1

I'm trying to add FB xmlns attribute to the document's <html> tag (<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">) dynamically. For some reason adding it like below does not work:

htmltag  = document.getElementsByTagName ('html');
htmltag[0].setAttribute("xmlns:fb","http://www.facebook.com/2008/fbml");");

How can I do it?

Thanks!

Update: No jquery or other lib is available.

1
  • 2
    Who should read that attribute? XML parsers don’t interpret JavaScript. Commented Aug 11, 2009 at 19:00

4 Answers 4

5

Heh actually, after doing the jquery solution I stated above;

I realized it could be done easier with a single javascript line:

document.documentElement.setAttribute("xmlns:fb", "http://www.facebook.com/2008/fbml");

Keep in mind ... If you view the source with the browser, you will not see the attribute attached, as it programatically adds it after the page loads. Use Firebug in firefox, or something that lets you see the source being manipulated on the fly, and you should be good to go.

Sign up to request clarification or add additional context in comments.

Comments

4

This probably isn't the answer you're wanting :)

Tacking on semantic markup with client-side code is not good practice. It hides the valuable machine-readable info from most machines. The HTML is already fetched, parsed and displayed by the time Javascript executes*. A dollar short and a day late!

If you can add the attributes on the server, before it is sent to the browser, go for it.

If you have to do it client-side; don't be tempted by a framework like jQuery or whatever. That's a huge overhead for a small task.

*non-scientific tests

Comments

4

This can be done very quickly with jquery.

Include:

(or point to a local jquery library)

and here's the code:

<script type="text/javascript">
$(document).ready(function() {
    $("html").attr("xmlns:fb", "http://www.facebook.com/2008/fbml");
});  </script>

1 Comment

Can't use jQuery in this case.
0

To get reference to the <html /> element use

document.documentElement

2 Comments

In case of an HTML document it’s the same as what Nir wrote: document.getElementsByTagName('html')[0] === document.documentElement evaluates to true.
You're absolutely right. The real problem is adding a namespace dynamically, which seems to make little sense in the context of a web page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.