Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 29th, 2004, 10:10 AM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Read HTML comments with Javascript

Does anyone know how to read HTML comments with javascript?
ie. I have a section in my HTML like
<div id="test">
<!--
Some words
-->
</div>

I want to get "some words" into a javascript variable.
I need this to be cross browser compatable.
I have read books and looked on the net and I think what I want is possable but I cannot make it work.
Thanks
Ben

  #2 (permalink)  
Old October 29th, 2004, 11:50 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ben,

You can use the innerHTML property of the <div> tag like this:

alert(document.getElementById('test').innerHTML);

You'll need to do some string parsing to get rid of the comment tags, but that shouldn't be too hard.

HtH,

Imar
  #3 (permalink)  
Old October 29th, 2004, 11:59 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

If you just want to set aside some text, it might be easier to use the CSS display property.

<div id='test' style='display: none;'>
    Some words.
</div>

<script type='text/javascript'>
    // Then just access the contents of that div like this
    document.getElementById('test').innerHTML;
</script>

The text in the <div> isn't visible in CSS capable browsers.

Regards,
Rich

--
[http://www.smilingsouls.net]
[http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail
  #4 (permalink)  
Old October 29th, 2004, 12:50 PM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,
Thanks for the sugestion, works a treat :-)
Can't beleive I did not try that, I got distracted by trying to directly get at the comment tag itself.

Richard,
Thanks for the Sugestion. Unfortunately I can't just "hide" the text, I need it ignored completely until I want to use it.


Thanks:-)



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read javaScript variables's values in brows AccessAasif@aol.com C# 0 October 15th, 2007 10:21 AM
javascript calendar won't appear unless HTML = out crmpicco Javascript How-To 1 May 9th, 2007 05:48 AM
VBA Formatting cells with Html tags read from XML hemagiri Excel VBA 0 November 22nd, 2006 02:41 AM
can't read value from select tag using javascript kumar_kumar Javascript 2 February 9th, 2005 11:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.