• About IR Thoughts

IR Thoughts

~ Thoughts on Information Retrieval, Data Mining, and Search Engines

IR Thoughts

Daily Archives: December 3, 2012

Snippet for showing HTML code on Web Pages

03 Monday Dec 2012

Posted by egarcia in Data Mining, Programming, Scripts

≈ Leave a Comment

I’m working on a data mining framework library and hit an old problem revisited many times across the Web: how to display HTML code on a page. Thanks to JQuery Syntax Highlighter and similar editors, this is easy to do.

This prompted me to search for even a simpler solution without all the nice features of these editors, but one that just does the mere work of showing code. I came up with the following strategy, tested on recent versions of IE, Opera, Chrome, Safari, and Firefox. I haven’t tested it with older browser versions and cannot claim it will work with those. More tests are necessary to please other programming/validation requirements.

1. Place HTML code into a hidden textarea.

2. Show  it with the following javascript one-line snippet

document.getElementById(‘id’).innerHTML=”<pre><code>”+document.getElementsByTagName(‘textarea’)[index].innerHTML+”</code></pre>”;

Optionally, both the textarea and snippet can be placed right before the closing body tag.

In the snippet, id is the identifier given to a tag container used to show the html code. This container can be a division placed at will in the body section of the document and CSS styled to your heart needs. The index parameter is the index value of the textarea; i.e. [0] if there is only one textarea in the page, [1] if it is the second textarea, and so forth.

When the textarea is replaced by a noscript tag, the snippet seems to work with the above browsers, except with firefox (particularly if the html code itself contains script tags).

PS.

If you care about semantics and SEO, simply move the <pre> and <code> tags from the snippet to the division container. Then add the id to the code tag instead of to the division container, like this:

<div><pre><code id=’id’></code></pre></div>

This makes the snippet even shorter, like this:

document.getElementById(‘id’).innerHTML=document.getElementsByTagName(‘textarea’)[index].innerHTML;

This isolates programming from presentation, which is always a good practice.

PS2 I added single quotes in snippet, so if you simply copy/paste it won’t flag you an error.

December 2012
M T W T F S S
« Nov   Jan »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Favorite Sites

  • Mi Islita

Pages

  • About IR Thoughts

Categories

  • AIRWeb Course
  • Conferences
  • Data Mining
  • Dynamics
  • Fractal Geometry
  • Graduate Courses
  • Hacking
  • Homeland Security
  • Human-Computer Interaction
  • Image Compression
  • Internet Engineering
  • IR Quizzes
  • IR Tools
  • IR Tutorials
  • Latent Semantic Indexing
  • Legacy Posts
  • Machine Learning
  • Marketing Research
  • Miscellaneous
  • News
  • Newsletters
  • Programming
  • Quack Science
  • Queries
  • Scripts
  • Search Engines Architecture Course
  • SEO Myths
  • Software
  • Spam
  • Statistics and Mathematics
  • Theses
  • Vector Space Models
  • Web Mining Course

Recent Posts

  • “Powered by” in Spanish
  • Some nice features added to the Image Crawler
  • The Images Crawler
  • A nice service for my locals
  • An update to the Web Crawler
  • New similarity measures
  • The Web Crawler is Back!
  • Tracking Users: An Email Crawler on Steroids
  • The Email Crawler: A Tool for Gathering Emails
  • The Binary Distance Calculator – a tool for comparing binary sets
  • Fractalettes: A Fractal Design Strategy to Color Mining and Learning through Discovery
  • AZZOO and WAZZOO: New Similarity Measures for the 21st Century
  • The Binary Similarity Calculator
  • From Harlem Shake to Link Shake: The Qualified Links Shake
  • Web Vulnerabilities and Search Engines

Archives

  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007

AIRWeb Course Conferences Data Mining Fractal Geometry Graduate Courses Hacking Homeland Security Human-Computer Interaction Internet Engineering IR Quizzes IR Tools IR Tutorials Latent Semantic Indexing Legacy Posts Machine Learning Marketing Research Miscellaneous Newsletters Programming Quack Science Queries Scripts Search Engines Architecture Course SEO Myths Software Spam Statistics and Mathematics Theses Vector Space Models Web Mining Course

Blog at WordPress.com. Theme: Chateau by Ignacio Ricci.