Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-09-2002, 12:17 PM   PM User | #1
hiuchan
New to the CF scene

 
Join Date: Jul 2002
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
hiuchan is an unknown quantity at this point
Unhappy How to read text in HTML

I tried to post the same subject in the Javascript forum but couldn't get the solution after a couple good members tried to help out. I was advised to post the same question in the DOM Scripting forum so here it is:

My question is, if I want to extract content (text) in a HTML file, how can I do it? For example, part of the HTML file will look like this:

...
<font size="3">
Hello!
</font>
...

I want to know how to extract the word "Hello".

The suggestion I got was to modify the HTML file to use <font id="id name"> and write a few line of Javascript to extract the content "Hello" based on the "id name". However, the most important thing is, I am only allowed to append Javascript function(s) to the **ORIGINALLY EXISTING** HTML file so I can't change the original <font size="3"> tag to <font id="id name"> tag.

Any suggestion please kindly let me know. If you think it is not possible to do it, I would also like to know.

Thanks!
hiuchan is offline   Reply With Quote
Old 07-09-2002, 01:01 PM   PM User | #2
Vladdy
Senior Coder

 
Join Date: Jun 2002
Location: Nashua, NH
Posts: 1,724
Thanks: 0
Thanked 0 Times in 0 Posts
Vladdy is an unknown quantity at this point
Let me start by saying that font tag is not a really good example here, since it is depreciated and should not be used when coding with DOM compatibility in mind (it woudl still work, but.....)

You can access any text on your page by going through the nodes tree. If you need a quick access to a given element, the best way is to give it an id and use document.getElementById method.
__________________
Vladdy | KL
"Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"
Vladdy is offline   Reply With Quote
Old 07-09-2002, 01:12 PM   PM User | #3
Bosko
Regular Coder

 
Join Date: Jun 2002
Location: The Netherlands
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Bosko is an unknown quantity at this point
But what if the nodes don't have ID's?
If the DOM tree of the html page stays the same,so that you know how many font tags there are then you can use this:
var test=document.getElementsByTagName('font');
test[0].innerHTML="blah";
Bosko is offline   Reply With Quote
Old 07-09-2002, 07:23 PM   PM User | #4
hiuchan
New to the CF scene

 
Join Date: Jul 2002
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
hiuchan is an unknown quantity at this point
Yes, this will solve my problem. In my HTML page, I will always have one and only one special content after the font tag which I need to locate and it will stay the same each time. In my example, I used the word "Hello" to represent this special static content.

Therefore the getElementsByTagName('font') will work if I add the for loop:

var test=document.getElementsByTagName('font');
for (var i=0; i<test.length; i++) {
if (test[i].innerHTML == 'Hello')
alert(test[i].innerHTML);
else
alert('no found yet');
}

Hmmm...I guess I am right?! Thanks for your help, guys!
hiuchan is offline   Reply With Quote
Old 08-22-2005, 07:07 PM   PM User | #5
jim1061
New to the CF scene

 
Join Date: Aug 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jim1061 is an unknown quantity at this point
extracting specific information from HTML documents

Hello,

I am trying to write code that will extract information from an html page and print the information. The html page will list codes a company uses to identify a wide range of colors in their available swatches. The html page allows a customer to drag and drop colors on to pictures and when done the customer can print out the page (final picture) and bring it in to the company for ordering. Within the HTML are the colors used to create the final picture.

I am a novice with Javascript but am somewhat familiar with code structure. Each color that can be used has a two letter prefix and there are only two possibilities (BW and BS). I need to insert a script that provides a button that when clicked will write a report of the colors in the html file. Is it possible to write Javascript that will look for BW## or BS##, collect those codes and then print them out when the button is selected?

I'll need to customize it and may ask for further assistance but right now I'm trying to work that script which will simply print a list of all BS and BW colors used within the web page. Any help is much appreciated.

Jim

Last edited by jim1061; 08-22-2005 at 07:09 PM..
jim1061 is offline   Reply With Quote
Old 08-22-2005, 07:19 PM   PM User | #6
jscheuer1
Regular Coder

 
Join Date: Mar 2005
Location: SE PA USA
Posts: 373
Thanks: 0
Thanked 0 Times in 0 Posts
jscheuer1 is an unknown quantity at this point
When it comes to printing specific page contents in specific ways, a css stylesheet with the media="print" attribute specified can work wonders. Sections of the page can be excluded by setting their display to none in this stylesheet. Items that you do not want shown live but want printed can have their style set display none in the regular stylesheet and to display block in the media="print" one.
jscheuer1 is offline   Reply With Quote
Old 08-22-2005, 08:44 PM   PM User | #7
jim1061
New to the CF scene

 
Join Date: Aug 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jim1061 is an unknown quantity at this point
CSS suggestion

"Items that you do not want shown live but want printed can have their style set display none in the regular stylesheet and to display block in the media="print" one"

I'm not trying to hide anything "live". Rather I see the code contains my colors and I just want to extract them and have them printed out. My needs become a bit more customized too in that there are sections of the picture "blocked" and labeled... in other words 3 or 4 sections of the photo are blocked together and those regions are designated a letter (ie:A or B or C) where when one color is dropped in a section all of the "blocked" regions get filled with the color. The final printout will not only need to list all colors used but which region of the photo they appeared in so the company has an accurate record of what the customer is looking for and retains the combinations accurately. Its all extracting data and printing so in its simplest form I was looking for javascript that is first able to pull out the information. Once that is accomplished the code must be made a little more fancy to extract the color codes and list them with the regions they are associated with.

Jim
jim1061 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:27 PM.


Advertisement
Log in to turn off these ads.