Connecting Tech Pros Worldwide Help | Site Map

body.innerHTML issue

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 11:23 AM
Reed
Guest
 
Posts: n/a
Default body.innerHTML issue

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!

  #2  
Old July 23rd, 2005, 11:23 AM
Vincent van Beveren
Guest
 
Posts: n/a
Default Re: body.innerHTML issue

> bodyText transilates it into[color=blue]
> <input name=email type=text id=email>
>
> Is there anything i can do to fix this?[/color]

You need to escape it.

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

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

should work



  #3  
Old July 23rd, 2005, 11:23 AM
Richard Cornford
Guest
 
Posts: n/a
Default Re: body.innerHTML issue

Reed wrote:[color=blue]
> 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>[/color]

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

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.


  #4  
Old July 23rd, 2005, 11:23 AM
Randy Webb
Guest
 
Posts: n/a
Default Re: body.innerHTML issue

Vincent van Beveren wrote:
[color=blue][color=green]
> > bodyText transilates it into
> > <input name=email type=text id=email>
> >
> > Is there anything i can do to fix this?[/color]
>
> You need to escape it.
>
> This is most easly done with regular expressions (I believe)
>
> bodyText = bodyText.replace(/\"/g,"\\\"");
>
> should work[/color]

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


--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
  #5  
Old July 23rd, 2005, 11:24 AM
Grant Wagner
Guest
 
Posts: n/a
Default Re: body.innerHTML issue

Richard Cornford wrote:
[color=blue]
> Reed wrote:[color=green]
> > 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?[/color]
>
> 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.[/color]

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 <gwagner@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_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-deve...upgrade_2.html


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.