0

I have a dynamically generated webpage through javascript, when I try to get html code of it using $("html").html(). I get everything, and it is fine. The only problem is for the empty tags such as <link />, <input />, <img />, <br /> are not displayed as I want.

I want them to be displayed in properly closed format as below -

<input type="text" name="nm" />

but they are getting displayed as

<input type="text" name="nm">

i.e. no closing

I want to create an html object in java of this string but I get an exception like invalid html.

Any solution ?

2

1 Answer 1

1

I want them to be displayed in properly closed format as below

That's only "proper" in XHTML. It's merely tolerated in HTML. If you're doing this in a properly served, genuine XHTML document, the browser should be returning valid XML. (It probably doesn't, but it should.) If you're using XHTML tag soup (serving XHTML as text/html) or using any form of HTML (HTML4 strict, HTML4 transitional, HTML5, no doctype at all), the browser should return HTML (which doesn't have the extraneous solidus). More: http://www.w3.org/TR/html5/embedded-content-0.html#dom-innerhtml

In any case, this is down to the browser, not jQuery. As far as I'm aware, you can't ask for a specific variant (other than specifying your doctype and serving with the correct content-type, which probably still isn't sufficient with today's browsers as yet).

Sign up to request clarification or add additional context in comments.

4 Comments

I want to create an html object in java of this string but I get an exception like invalid html.
@MotaBOS: You're using jQuery to serialize a DOM structure to an HTML string, and then feeding that string into a Java program that's throwing an error claiming it's malformed? There's certainly nothing malformed about <input type="text" name="nm"> in HTML. It sounds like you need to ensure the Java parser you're using is in HTML mode, not XHTML mode. If it doesn't have one, perhaps look at another (JSoup is popular). Or, sadly, post-process or do your own serialization in JavaScript (which sounds bad, but isn't really all that hard; a recursive-descent function and proper escaping...).
Thanks, that's enough help for me.
@MotaBOS: Good luck with it! Best,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.