I have one linked css file for default properties and a second one for the news that it changes when the wiewport is changed. At the end of the JS script I puted an element's offsetWidth in the global value NavW. So JS seems to returns the wrong value, it doesn't compute only in the dom but based of the first default css properties not the new one. However, the target element rendered correctly. Notice that the value '2' in the condition is because there is a Link element which is not a css file but an icon.
function switchTheme(newHref)
{
var newIndex;
newIndex = document.getElementsByTagName("link").length;
var newTheme = document.createElement("link");
newTheme.setAttribute("rel", "stylesheet");
newTheme.setAttribute("type", "text/css");
newTheme.setAttribute("href", newHref);
if ( newIndex == 2 )
{
document.getElementsByTagName("head").item(newIndex - 2).appendChild(newTheme);
}
else if ( newIndex > 2 )
{
var themeToReplace = document.getElementsByTagName("link").item(2);
document.getElementsByTagName("head").item(0).replaceChild(newTheme, themeToReplace);
}
NavW = document.getElementById("wrap").offsetWidth;
}