最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's up with innerHTML and <embed>? - Stack Overflow

programmeradmin0浏览0评论

I'm trying to fix a bug in a rich text editor I'm using, which causes <embed> tags to be inserted without their closing tag (which screws the output up pletely). I've isolated the problem to this operation:

// body is a <body> tag
body.innerHTML = '<embed src=""></embed>';

No fancy code, just Firefox's innerHTML assignment. You should be able to duplicate the error in Firebug like so:

>>> document.body.innerHTML = "<embed></embed>"
"<embed></embed>"
>>> document.body.innerHTML
"<embed>"

Is there a workaround for this? I need the tag, but I can't justify rebuilding/replacing the entire rich text editor because of one crappy edge case.

I can't convert this to something like document.createElement('embed'), because real-world input to this editor can easily include a few paragraphs of text wrapped around the <embed>; innerHTML is, on paper, perfect for this use case, I just can't get it to work with an <embed>.

I'm trying to fix a bug in a rich text editor I'm using, which causes <embed> tags to be inserted without their closing tag (which screws the output up pletely). I've isolated the problem to this operation:

// body is a <body> tag
body.innerHTML = '<embed src="http://example./whatever"></embed>';

No fancy code, just Firefox's innerHTML assignment. You should be able to duplicate the error in Firebug like so:

>>> document.body.innerHTML = "<embed></embed>"
"<embed></embed>"
>>> document.body.innerHTML
"<embed>"

Is there a workaround for this? I need the tag, but I can't justify rebuilding/replacing the entire rich text editor because of one crappy edge case.

I can't convert this to something like document.createElement('embed'), because real-world input to this editor can easily include a few paragraphs of text wrapped around the <embed>; innerHTML is, on paper, perfect for this use case, I just can't get it to work with an <embed>.

Share Improve this question edited Sep 16, 2010 at 21:34 ojrac asked Jan 12, 2009 at 21:31 ojracojrac 13.4k6 gold badges39 silver badges40 bronze badges 2
  • Would it work for you if you create <object><embed></object> ? Will it still foobar your content? – mark Commented Jan 12, 2009 at 21:49
  • mfn -- wrapping the <embed> in an <object> made it not show at all in Google Chrome, but (oddly enough) wrapping it in a <p> was enough to fix the display problems in RSS readers. If you post that as an answer, I'll give you the credit. – ojrac Commented Jan 12, 2009 at 23:12
Add a ment  | 

3 Answers 3

Reset to default 6

This might not be an answer to your problem, but <embed> was not part of any standardized version of HTML until HTML5, according to W3C.

Although <embed> is not a part of a standard html, it has been widely implemented and is, therefore, a de-facto standard.

That said, I'm running Firefox 3 (MineField v3.0.0.5) and am getting slightly unexpected, but altogether correct functionality. I was expecting the exact HTML to be put into the element I selected, but instead I got clean html.

Since <embed is an empty tag (that is, it cannot have contents, only attributes), Firefox turns this

<embed attribute="value"></embed>

into

<embed  attribute="value"/>

, which is a prefectly valid closed tag. It does the same on other empty tags, such as <input /> and <image>

At the this page you can see this in action. The scriptless version of the page has an embedded video (youtube) inside of a bright pink div. The script steals its contents and places it into the body as innerHTML.

This reminds me of the problems document.write had/has with <script /> tags.

Lies. Don't answer SO questions right before bed.

I tried the following in Firebug

//break up initial embed into single chunks
document.body.innerHTML = "<e"+"mbe"+"d></embed>" 

and got the expected to show up when I checked the value of innerHTML. The idea here is to break up the initial string into multiple concatenated strings. Stupid, but a possible work-around.

发布评论

评论列表(0)

  1. 暂无评论