home  |  book  |  blogs  |   RSS  |  contact  |
  An American Manifesto
Saturday February 4, 2012 
by Christopher Chantrill Follow chrischantrill on Twitter

TOP NAV

Home

Blogs

Opeds

Articles

Bio

Contact

BOOK

Manifesto

Sample

Faith

Education

Mutual aid

Law

Books

BLOGS 12

Feb 2012

Jan 2012

BLOGS 11

Dec 2011

Nov 2011

Oct 2011

Sep 2011

Aug 2011

Jul 2011

Jun 2011

May 2011

Apr 2011

Mar 2011

Feb 2011

Jan 2011

BLOGS 10

Dec 2010

Nov 2010

Oct 2010

Sep 2010

Aug 2010

Jul 2010

Jun 2010

May 2010

Apr 2010

Mar 2010

Feb 2010

Jan 2010

BLOGS 09

Dec 2009

Nov 2009

Oct 2009

Sep 2009

Aug 2009

Jul 2009

Jun 2009

May 2009

Apr 2009

Mar 2009

Feb 2009

Jan 2009

BLOGS 08

Dec 2008

Nov 2008

Oct 2008

Sep 2008

Aug 2008

Jul 2008

Jun 2008

May 2008

Apr 2008

Mar 2008

Feb 2008

Jan 2008

BLOGS 07

Dec 2007

Nov 2007

Oct 2007

Sep 2007

Aug 2007

Jul 2007

Jun 2007

May 2007

Apr 2007

Mar 2007

Feb 2007

Jan 2007

BLOGS 06

Dec 2006

Nov 2006

Oct 2006

Sep 2006

Aug 2006

Jul 2006

Jun 2006

May 2006

Apr 2006

Mar 2006

Feb 2006

Jan 2006

BLOGS 05

Dec 2005

Nov 2005

Oct 2005

Sep 2005

Aug 2005

Jul 2005

Jun 2005

May 2005

Apr 2005

Mar 2005

Feb 2005

Jan 2005

BLOGS 04

Dec 2004

Dont Forget Most Old Media is Union Shop Are the Democrats for Real

print view

AJAX and The innerHTML Problem

by Christopher Chantrill
January 10, 2007 at 3:21 am

HERE AT Road to the Middle Class we are nothing if not modern.  That means that the site ought to be LAMP compliant, using Linux, Apache, MySql, and PHP, and so it does.  But now, according to The Economist and Eric Schmidt, CEO of Google, it appears that a truly modern site should use AJAX as well.  AJAX (Asynchronous JavaScript and Xml) is the technology that Google uses on GMail.

Well, how hard could it be?

The standard demonstration of AJAX on W3Schools.com shows how to use an AJAX setup to update text hints on the fly.  You press a key to enter a character in a text box and every time you release the key your page requests a new text hint from the server with an HttpRequest.

When the response comes back from the server you update the text hint element like this:

document.getElementById("txtHint").innerHTML =xmlHttp.responseText

And everyone is happy.

But there’s a problem.  The best use of AJAX here at Road to the Middle Class is to fold/unfold blog entries.  We conserve real estate by showing only the first 50 words of a blog and allow you to click a link to get the whole article.  But that means that the content coming back from the server is going to include HTML markup.  What happens if the responseText isn’t just simple text but includes embedded HTML? 

Good question.  It turns out, using the W3Schools example, that embedded HTML works on Firefox but not on our friends at Internet Explorer: IE6 and IE7.  The IE boys take one look at the embedded HTML and decide that it’s not a job for innerHTML.  They return an "unknown runtime error".

Here’s how you update your page with AJAX when the responseText is complex and includes embedded HTML as it is here at Road to the Middle Class.  I call it the Double Span Solution.

First of all, you surround your text with two <span> elements, like this:

<p><span id=blog101><span id=blog101s>
<p>Text including a <a href="">link</a> and stuff.</p>
</span></span>
</p>

The idea is that when you want to replace the text in the middle of the two <span> elements you replace the entire inner <span> element.  You delete the old <span> and add a new <span> with the responseText and all of its HTML markup.

First of all you set up the new <span> element, preserving the element id from the old <span> element and adding in the responseText from the server.

var elem = document.getElementById("blog101s")
var newSpan = document.createElement(’span’)
newSpan.id = elem.id
newSpan.innerHTML = xmlHttp.responseText

Now remove the "blog101s" element and anything else floating around.

for (var i = 0; i < document.getElementById("blog101").childNodes.length; i++) {
   var n = document.getElementById("blog101").childNodes[i]
   n.parentNode.removeChild(n)
}

OK.  Now append the new <span> element right under "blog101".

document.getElementById(gBlogid).appendChild(newSpan)

And that should do it.

Technorati tags: , ,

Sphere: Related Content |

Christopher Chantrill blogs at www.roadtothemiddleclass.com.  His Road to the Middle Class is forthcoming.


Comments:


Posted by: Robert Goretsky on 01/09/08 5:20pm

Thank you for this tip! I was trying to use innerHTML to add sub-elements of a list inside a span, and while it worked in Firefox, it did gave the error you mentioned in IE. Using this JS fixed it up!


 TAGS


What Liberals Think About Conservatives

[W]hen I asked a liberal longtime editor I know with a mainstream [publishing] house for a candid, shorthand version of the assumptions she and her colleagues make about conservatives, she didn't hesitate. “Racist, sexist, homophobic, anti-choice fascists,” she offered, smiling but meaning it.
Harry Stein, I Can't Believe I'm Sitting Next to a Republican


US Life in 1842

Families helped each other putting up homes and barns. Together, they built churches, schools, and common civic buildings. They collaborated to build roads and bridges. They took pride in being free persons, independent, and self-reliant; but the texture of their lives was cooperative and fraternal.
Michael Novak, The Spirit of Democratic Capitalism


Society and State

For [the left] there is only the state and the individual, nothing in between. No family to rely on, no friend to depend on, no community to call on. No neighbourhood to grow in, no faith to share in, no charities to work in. No-one but the Minister, nowhere but Whitehall, no such thing as society - just them, and their laws, and their rules, and their arrogance.
David Cameron, Conference Speech 2008


Socialism equals Animism

Imagining that all order is the result of design, socialists conclude that order must be improvable by better design of some superior mind.
F.A. Hayek, The Fatal Conceit


Sacrifice

[Every] sacrifice is an act of impurity that pays for a prior act of greater impurity... without its participants having to suffer the full consequences incurred by its predecessor. The punishment is commuted in a process that strangely combines and finesses the deep contradiction between justice and mercy.
Frederick Turner, Beauty: The Value of Values


Religion, Property, and Family

But the only religions that have survived are those which support property and the family. Thus the outlook for communism, which is both anti-property and anti-family, (and also anti-religion), is not promising.
F.A. Hayek, The Fatal Conceit


Racial Discrimination

[T]he way “to achieve a system of determining admission to the public schools on a nonracial basis,” Brown II, 349 U. S., at 300–301, is to stop assigning students on a racial basis. The way to stop discrimination on the basis of race is to stop discriminating on the basis of race.
Roberts, C.J., Parents Involved in Community Schools vs. Seattle School District


Postmodernism

A writer who says that there are no truths, or that all truth is ’merely relative’, is asking you not to believe him. So don’t.
Roger Scruton, Modern Philosophy


Physics, Religion, and Psychology

Paul Dirac: “When I was talking with Lemaître about [the expanding universe] and feeling stimulated by the grandeur of the picture that he has given us, I told him that I thought cosmology was the branch of science that lies closest to religion. However [Georges] Lemaître [Catholic priest, physicist, and inventor of the Big Bang Theory] did not agree with me. After thinking it over he suggested psychology as lying closest to religion.”
John Farrell, “The Creation Myth”


Pentecostalism

Within Pentecostalism the injurious hierarchies of the wider world are abrogated and replaced by a single hierarchy of faith, grace, and the empowerments of the spirit... where groups gather on rafts to take them through the turbulence of the great journey from extensive rural networks to the mega-city and the nuclear family...
David Martin, On Secularization


Never Trust Experts

No lesson seems to be so deeply inculcated by the experience of life as that you should never trust experts. If you believe doctors, nothing is wholesome: if you believe the theologians, nothing is innocent: if you believe the soldiers, nothing is safe. They all require their strong wine diluted by a very large admixture of insipid common sense.
Lord Salisbury, “Letter to Lord Lytton”


Mutual Aid

In 1911... at least nine million of the 12 million covered by national insurance were already members of voluntary sick pay schemes. A similar proportion were also eligible for medical care.
Green, Reinventing Civil Society


mysql close 0

 

©2007 Christopher Chantrill