Question

I use this code to insert <bgsound> tag.

EmbeddedWB.Doc2.body.insertAdjacentHTML('afterBegin','<bgsound src="chimes.wav">');

Ignore for a second that <bgsound> is obsolete. I am working with it because in IE6 it works.

But nothing is present in the HTML source after above operation. At first I assumed something is wrong with above code until I tried this:

EmbeddedWB.Doc2.body.insertAdjacentHTML('afterBegin','<p>chimes.wav</p>');

At the above it correctly inserts <p> tag just after the <body> as it should. So why it won't insert <bgsound> then?

Also, something weird happens if I insert <audio> tag instead:

EmbeddedWB.Doc2.body.insertAdjacentHTML('afterBegin','<audio src="chimes.wav">some audio</audio>');

After this in source all I see is some audio</audio> without the opening tag.

I am beginning to wonder if the above doesn't work because I am in designer mode, is this possible cause why it won't insert <bgaudio>? Tried to insert some other tags too... for example comments also didn't work (<!-- comment -->).

Edit: Later I tried FEATURE_BROWSER_EMULATION in registry and after setting to 11001 (0x2AF9 Internet Explorer 11 mode) both <audio> and <bgsound> tags were inserted properly from edit mode. But it must be possible in older mode doesn't it?

Was it helpful?

Solution

After a lot of wasted time here are some things I discovered:

  • Internet Explorer control strips tags which it thinks are invalid when using insertAdjacentHTML or even innerHTML. There doesn't seem to be a way (that I know of) to insert any custom HTML code which the control will not try to validate. In IE6 where I was testing it it thinks that <bgsound> and <audio> tags are invalid so it strips them. It should not do that for <bgsound> but it does. Same goes for comments like <!-- comment -->.

  • I found no way to insert the tag using insertAdjacentHTML or innerHTML. The best I could do is to wrap the sound tag inside some other tag - if I do that, the sound does play when inserted but it is still absent from source. Finally I inserted it directly into generated HTML before saving the HTML to disk (I tagged that sound was added and before saving HTML I inserted it into HTML without relying on MSHTML).

  • Switching to standards mode e.g. by using registry entry FEATURE_BROWSER_EMULATION correctly inserts tags but won't work on some versions of Internet Explorer (I believe the minimum version would be IE9 for this to work) so it is less flexible.

I found no solution for this but at least I have some workarounds. Perhaps it helps someone in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top