I found out, that document.getElementById doesn't see ids of these elements, that are placed in some namespace. At least in FF30.0 and IE11 (don't know about other browsers); Consider this JSP snippet (to force Content-Type; probably it would work with meta http-equiv also):
HTML:
<!DOCTYPE html>
<%@ page language="java" contentType="application/xhtml+xml; charset=UTF-8" pageEncoding="UTF-8" %>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:t="urn:test">
<head>
<meta charset="UTF-8"/>
<title>Test</title>
<style type="text/css">
@namespace "http://www.w3.org/1999/xhtml";
@namespace t "urn:test";
html {
font-family: 'Open Sans', 'Calibri', 'Arial', sans-serif;
font-size: 11px;
}
t|foo {
display: inline-block;
border: solid 1px #AAA;
border-radius: 2px;
background-color: #EEE;
padding: 0px 3px;
}
</style>
JS:
function init() {
var NS_TEST = 'urn:test';
var NS_HTML = 'http://www.w3.org/1999/xhtml';
var foo = document.getElementById('foo');
console.log('foo!=null?' + (foo !== null));
var foos = document.getElementsByTagNameNS(NS_TEST, 'foo');
console.log('foos.length=' + foos.length);
// assert foos.length == 1;
foo = foos[0];
console.log('foo.id : ' + foo.id);
console.log('foo.getAttribute() : ' + foo.getAttribute('id'));
console.log('foo.getAttributeNS(TEST): ' + foo.getAttributeNS(NS_TEST, 'id'));
console.log('foo.getAttributeNS(HTML): ' + foo.getAttributeNS(NS_HTML, 'id'));
}
window.onload = init;
HTML:
<body>
<div>
<t:foo id="foo">Foo indeed</t:foo>
</div>
</body>
</html>
In both mentioned browsers t:foo element gets styled according to CSS rules, but note console output:
FF:
foo!=null?false
foos.length=1
foo.id : foo
foo.getAttribute() : foo
foo.getAttributeNS(TEST): null
foo.getAttributeNS(HTML): null
IE:
foo!=null?false
foos.length=1
foo.id : undefined
foo.getAttribute() : foo
foo.getAttributeNS(TEST):
foo.getAttributeNS(HTML):
Both browsers return false on foo != null, but getting the element with document.getElementsByTagNameNS actually finds it in DOM and in both cases foo.getAttribute('id') returns valid id. Note, that it does not exists in http://www.w3.org/1999/xhtml namespace thou default namespace is specified in html element. Does anyone have any idea how to get elements with defined id for elements placed in some namespace like in the example above? Or maybe I need to add some extra declaration (<?xml ...?> didn't help).