473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

innerHTML and <br>

Hi

on the same page I set the innerHTML property of an element to something
like

my_string + "<br />"

then when I try to remove my_string + "<br />" from the innerHTML it
fails since the "<br />" is not found.

after some debugging I found out that the innerHTML actually contains:

my_string + "<br>"

This happens in Firefox... in Konqueror it's even different:

my_string + "<BR>"

is this the expected behavior (i.e., browser dependent)... is there a
way to find out how <bris actually stored into an innerHTML?

Or is what I'm trying to do a design error?

thanks in advance
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DSI, Univ. di Firenze
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Feb 7 '07 #1
7 9729
Lorenzo Bettini wrote:
then when I try to remove my_string + "<br />" from the innerHTML it
fails since the "<br />" is not found.

after some debugging I found out that the innerHTML actually contains:

my_string + "<br>"

This happens in Firefox... in Konqueror it's even different:

my_string + "<BR>"

is this the expected behavior (i.e., browser dependent)... is there a
way to find out how <bris actually stored into an innerHTML?
The browser, when you set innerHTML, parses your snippet of markup into
nodes, when you read innerHTML, it serializes the nodes to a snippet of
markup.
If you want to know whether your element contains a br element then use e.g.
element.getElementsByTagName('br').length 0
where element is the element you originally checked the innerHTML on.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 7 '07 #2
Lorenzo Bettini said the following on 2/7/2007 8:14 AM:
Hi

on the same page I set the innerHTML property of an element to something
like

my_string + "<br />"
Don't try to insert XHTML with innerHTML, it doesn't work.
then when I try to remove my_string + "<br />" from the innerHTML it
fails since the "<br />" is not found.

after some debugging I found out that the innerHTML actually contains:

my_string + "<br>"
That's because the browser normalized the XHTML tag to HTML.
This happens in Firefox... in Konqueror it's even different:

my_string + "<BR>"
Only the case is different. The tag is the same.
is this the expected behavior (i.e., browser dependent)... is there a
way to find out how <bris actually stored into an innerHTML?
Use toLowerCase on your string then it is all lowercase.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '07 #3
Martin Honnen wrote:
Lorenzo Bettini wrote:
>then when I try to remove my_string + "<br />" from the innerHTML it
fails since the "<br />" is not found.

after some debugging I found out that the innerHTML actually contains:

my_string + "<br>"

This happens in Firefox... in Konqueror it's even different:

my_string + "<BR>"

is this the expected behavior (i.e., browser dependent)... is there a
way to find out how <bris actually stored into an innerHTML?

The browser, when you set innerHTML, parses your snippet of markup into
nodes, when you read innerHTML, it serializes the nodes to a snippet of
markup.
If you want to know whether your element contains a br element then use
e.g.
element.getElementsByTagName('br').length 0
where element is the element you originally checked the innerHTML on.
but this actually returns 1, while I should know the actual contents of
that element (i.e., <bror <BR>)

--
Lorenzo Bettini, PhD in Computer Science, DSI, Univ. di Firenze
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Feb 7 '07 #4
Lorenzo Bettini wrote:
Martin Honnen wrote:
>If you want to know whether your element contains a br element then
use e.g.
element.getElementsByTagName('br').length 0
where element is the element you originally checked the innerHTML on.

but this actually returns 1, while I should know the actual contents of
that element (i.e., <bror <BR>)
br is an _empty_ element, it does not have contents. The tag name of
elements in a HTML document is being normalized so while your markup
might use <bror <Bror <bRor <BRthe DOM will contain a normalized
form (e.g. BR or br). There is no way to find the original markup in the
DOM, unless you use XHTML and serve it as application/xhtml+xml so that
the browser uses its XML parser.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 7 '07 #5
VK
I should know the actual contents of
that element (i.e., <bror <BR>)
In HTML there is not such thing as "actual content", so you cannot get
something that doesn't exist. ;-)
HTML source served from the server - or from local file - is not what
you see on the screen. The original HTML source is only a "guideline"
for the UA parser. The visual or aural representation in any UA is the
result of i) normalization and ii) error correction of the original
source. Respectively the resulting DOM Tree may be very far from what
you could expect from the original source - and it never identical to
the said source.

This way if for your project is important to know if normalized node
contains <bror <BRor <br /then you are doing something against
the WWW nature.

You may query for two things:
1) Original HTML source fragment used as the basis for the given DOM
Tree node. For that you may use XHR and its responseText. I want to
stress once again that there is no equality of any kind between the
source and the resulted DOM Tree on a particular UA. This way the
practicality of such query would be limited by studies of
normalization and error correction mechanics of that particular UA.
2) Internal HTML code associated with the given DOM node. innerHTML,
getAttribute, nodeValue etc. may be used for that. Because such
internal code is the result of normalization and error correction of a
particular UA, you cannot make any theoretical predictions about the
reported form. You only can study normalization and error correction
per UAs and then make some educated guesses based on run-time UA
sniffing.

P.S. Such UAs' behavior is by design.

P.P.S. With proper project approach it has no practical impact because
in your script you are dealing with DOM Tree and not with textual
sources. So it should bother you no more than say the internal byte
order on your machine when you are using a calculator applet.

Feb 7 '07 #6
VK wrote:
>I should know the actual contents of
that element (i.e., <bror <BR>)

In HTML there is not such thing as "actual content", so you cannot get
something that doesn't exist. ;-)
HTML source served from the server - or from local file - is not what
you see on the screen. The original HTML source is only a "guideline"
for the UA parser. The visual or aural representation in any UA is the
result of i) normalization and ii) error correction of the original
source. Respectively the resulting DOM Tree may be very far from what
you could expect from the original source - and it never identical to
the said source.

This way if for your project is important to know if normalized node
contains <bror <BRor <br /then you are doing something against
the WWW nature.

You may query for two things:
1) Original HTML source fragment used as the basis for the given DOM
Tree node. For that you may use XHR and its responseText. I want to
stress once again that there is no equality of any kind between the
source and the resulted DOM Tree on a particular UA. This way the
practicality of such query would be limited by studies of
normalization and error correction mechanics of that particular UA.
2) Internal HTML code associated with the given DOM node. innerHTML,
getAttribute, nodeValue etc. may be used for that. Because such
internal code is the result of normalization and error correction of a
particular UA, you cannot make any theoretical predictions about the
reported form. You only can study normalization and error correction
per UAs and then make some educated guesses based on run-time UA
sniffing.
Basically what I'm doing is the following, append an error string to the
innerHTML of an element with a final <brcharacter, and then I need to
be able to remove that error string (together with the <br>); now I'm
using this solution (that considers both <brand <BR>):

// this function adds an error at the end of a possible previous
// error message
function addError(elementId, error, isErrorOrMsg)
{
// find the HTML element that displays the error
var message = document.getElementById(elementId + (isErrorOrMsg ? "Err"
: "Msg"));

if (message.innerHTML == '' || message.innerHTML.indexOf(error) == -1) {
// OK this error is not already shown, so add it and show the error
message.className = (isErrorOrMsg ? "error" : "msg");
message.innerHTML += error + '<br>';
// the error is appended to the possible current one
}
// otherwise this error is already shown
}

// this function removes an error from a possible previous
// error message. Then, if the error message is empty, also makes it hidden
function removeError(elementId, error, isErrorOrMsg)
{
// find the HTML element that displays the error
var message = document.getElementById(elementId + (isErrorOrMsg ? "Err"
: "Msg"));

// some browsers might change the case of <br>, e.g., Firefox
var toreplace = error + "<br>";
if (message.innerHTML.indexOf(toreplace) == -1) {
toreplace = error + "<BR>";
}

message.innerHTML = message.innerHTML.replace(toreplace, "");

if (message.innerHTML == '') {
// OK this error empty so we hide it
message.className = "hidden";
}
}

but, from what I understand, this design is flaw from the start right?

thanks in advance
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DSI, Univ. di Firenze
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Feb 7 '07 #7
On 7 Feb, 14:14, Lorenzo Bettini <bett...@dsi.unifi.itwrote:
Hi

on the same page I set the innerHTML property of an element to something
like

my_string + "<br />"

then when I try to remove my_string + "<br />" from the innerHTML it
fails since the "<br />" is not found.

after some debugging I found out that the innerHTML actually contains:

my_string + "<br>"

This happens in Firefox... in Konqueror it's even different:

my_string + "<BR>"

is this the expected behavior (i.e., browser dependent)... is there a
way to find out how <bris actually stored into an innerHTML?
There isn't any public standard that applies to this property.

Feb 8 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: fis | last post by:
Hi all, I've problem because there are needed break lines in my texts on the web site but i can't do it :( My pipeline looks like: XMS -> I18N -> XSLT -> HTML I have lot of texts in my...
6
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
7
by: noor.rahman | last post by:
I have an XML file that stores data from an HTML form. I use XSL to display the data in HTML format. The data may have newline characters. However, XSL is not displaying the newlines properly in...
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
2
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
1
by: susikaufmann2003 | last post by:
Hello, i have a html text, where it is possible (using ajax) to edit the text by clicking on it. The text is then shown in a textarea. My problem is, that the newlines are not displayed in the...
2
by: xhe | last post by:
I met a very headache problem in javascript, I think this might be difference between IE and NS / Safari. I have a text area <form> <textarea name='tex1' onkeyup='displayit();'></textarea>...
7
by: Nathan Sokalski | last post by:
Something that I recently noticed in IE6 (I don't know whether it is true for other browsers or versions of IE) is that it renders <br/and <br></br> differently. With the <br/version, which is what...
1
by: divina11 | last post by:
function sort() { c.sort() document.getElementById('display').innerHTML=""; document.getElementById('display').innerHTML=c +'<br>'; } // document.getElementById('display').innerHTML=c...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.