View Full Version : innerHTML resolving relative paths (IE only)

06-03-2006, 12:51 AM
i am having a problem when setting the innerHTML of a div with html that contains an img tag. the img tag contains a relative path in the src when i set the innerHTML but when i grab the value it has resolved it to the full url. this only happens in IE6.0 (well i've only tested that and firefox but i only need it to work in IE).

function Test()
var xElem = document.getElementById( "testing" ) ;
var sHTML = "<img src=\"/content/images/blank.jpg\" />" ;
alert( sHTML ) ;
xElem.innerHTML = sHTML ;
alert( xElem.innerHTML ) ;

<div id="testing"></div>
<input type="button" value="test" onclick="Test()">

what i want is for the paths to not resolve ie. stay the same as when i set it. instead i get <img src="http://localhost/content/images/blank.jpg"/>. i tried using the DOM methods (createTextNode, appendChild, etc) and i see the same results. i've searched and searched and can't find an answer...any help is greatly appreciated!

Bill Posters
06-03-2006, 06:55 AM
It may help to keep the url relative.
Try using a src url without the opening root slash. As you may or may not know, the root slash acts as shorthand for the full domain. A url with a root slash is effectively an absolute url, which is probably what IE is picking up on.

Alternatively, you could check the url string value returned for the presence of the domain string and, if present, split it into an array, taking just the part you need.
If the domain isn't present in the url string, then you should be able to take the url string value directly.


06-03-2006, 04:48 PM
thanks for your suggestions. i tried using the relative path "content/images/blank.jpg" but it still resolved the path to the full url. i thought about doing the domain replace but i'd rather not have to look for it. the app will go on different systems (dev, staging, production) and i'd hate to add code to allow us to specify which domain to look for on a specific system just for this. i just don't understand why IE wants to resolve the path...shouldn't the property just take the html i assign it and not touch it?

06-05-2006, 02:00 PM
nomatter which method you will use do get the url of an element, the result will be always the full url, not the relative. But why does it bother you? Why do you need the relative? What to do with it?

06-05-2006, 04:23 PM
the problem is that the app doing the changing is under one domain and the html will appear in another domain. i'm trying to create a simple app to allow some users to update a portion of a webpage in an editor (editor is in www.domain1.com and html will appear in www.domain2.com). i wouldn't expect either method to change the underlying html by resolving the paths for me. firefox doesn't have this behavior. if you take my code and run it on your machines, do you get the same results as i do?