body.innerHTML issue

R

Reed

I'm having a problem with apostrophes & quotes when using body.innerHTML.

With the statement: bodyText = document.body.innerHTML

If there was a Form object on my page such as:
<input name="email" type="text" id="email">

bodyText transilates it into
<input name=email type=text id=email>

Is there anything i can do to fix this?
Thanks!
 
V

Vincent van Beveren

bodyText transilates it into
<input name=email type=text id=email>

Is there anything i can do to fix this?

You need to escape it.

This is most easly done with regular expressions (I believe)

bodyText = bodyText.replace(/\"/g,"\\\"");

should work
 
R

Richard Cornford

Reed said:
I'm having a problem with apostrophes & quotes when
using body.innerHTML.

With the statement: bodyText = document.body.innerHTML

If there was a Form object on my page such as:
<input name="email" type="text" id="email">

bodyText transilates it into
<input name=email type=text id=email>

You have only tired that on one browser. Mozilla and Opera each produce
distinct output form innerHTML (as do Konqueror/Safari and IceBrowser).
Is there anything i can do to fix this?

No, the innerHTML property reports a string constructed as a normalised
representation of the underlying DOM and any one browser will produce
what it produces, while different browsers (and browser versions) will
produce a different interpretation. Particularly when it comes to
quotes, apostrophise and the case of tag names and attribute names.

Richard.
 
R

Randy Webb

Vincent said:
You need to escape it.

This is most easly done with regular expressions (I believe)

bodyText = bodyText.replace(/\"/g,"\\\"");

should work

That works if you already have the string. It doesn't do what the op is
wanting to do though. Try it out.

What the OP is wanting is to read the innerHTML and get the quoted value
back from the browser, which it is normalizing. To get the actual text
of it, with quotes and all, look into the HTTPRequestObject or the JAVA
component listed in the FAQ
 
G

Grant Wagner

Richard said:
No, the innerHTML property reports a string constructed as a normalised
representation of the underlying DOM and any one browser will produce
what it produces, while different browsers (and browser versions) will
produce a different interpretation. Particularly when it comes to
quotes, apostrophise and the case of tag names and attribute names.

Richard.

The thing to do to fix it is to not rely on attempting to parse the
contents of innerHTML. Instead, use the DOM accessor methods to obtain the
properties of the elements you want.

ie - document.getElementById('email'); to obtain a specific reference to
that input, or document.getElementsByTagName('input'); to obtain a
collection of all <input> tags, etc.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top