/    Sign up×
Community /Pin to ProfileBookmark

reading an URL, searching for <span …>

Hi,

Never done any JS before but used to Java. Is there an equivalent to Reader/readLine() for getting the source of a web page. I’m looking for the <span …>- tag and want to create Strings to send to the <prompt>-tag in a VoiceXML application. Is it possible?

Thanks for any advice

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@scragarDec 14.2004 — Never done any JS before but used to Java. Is there an equivalent to Reader/readLine() for getting the source of a web page. I'm looking for the <span ...>- tag and want to create Strings to send to the <prompt>-tag in a VoiceXML application. Is it possible?[/quote]

Do you want to tell me what Reader/readLine() does before we start?

you can quite easily read a spans contents, it does however require an id property.

<span id=First>text</span><script>

alert(First.innerHTML);

</script>

I've never used any VoiceXML though so I can't help you with the interaction...
Copy linkTweet thisAlerts:
@CharlesDec 14.2004 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<meta name="Content-Style-Type" content="text/css">

<title>Example</title>

<script type="text/javascript">

<!--

onload = function () {

var e, i = 0;

while (e = document.getElementById ('giant-says').getElementsByTagName ('SPAN')[i++]) {alert (e.firstChild.data)};

}

// -->

</script>

</head>

<body>

<p id="giant-says"><span>Fee</span>, <span>Fie</span>, <span>Foe</span>, <span>Fum</span>.</p>

</body>

</html>[/font]
Copy linkTweet thisAlerts:
@burdenauthorDec 14.2004 — First of all: Thanks.

scragar: Reader is an Object for reading files/URL:s. readLine() is the method for reading a file, in this case line by line.

I'm sorry if I was unclear about the <span>tag but it looks like this:

<span class="CLASSNAME">

guess I can use class as id?

charles: Isn't the alert(Expr) for pop-up windows?
Copy linkTweet thisAlerts:
@scragarDec 14.2004 — he was probly just using it as an example of the fact that you can grab it's contents.

you can still read them in the same way that charles did(although you kigh want to remove "getElementById ('giant-says')." as that might require you to add a little more code than nessesary.



<script type="text/javascript">

<!--

onload = function () {

var e, i = 0;

while (e = document.getElementsByTagName ('SPAN')[i++]) {

alert (e.firstChild.data)

};

};

// -->

</script>

<span class=CLASSNAME>Fee</span>, <span class=CLASSNAME>Fie</span>, <span class=CLASSNAME>Foe</span>, <span class=CLASSNAME>Fum</span>.



as for reading a file, that's fairly complexe...
Copy linkTweet thisAlerts:
@CharlesDec 14.2004 — As you have given us very little to go on, I assumed that you might have the SPAN elements in question aggregated under some other element. We want to be careful not to grab other SPANs. But if you want to use class names to distinguish your SPANs:

[font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<meta name="Content-Style-Type" content="text/css">

<title>Example</title>

<script type="text/javascript">

<!--

onload = function () {

var e, i = 0;

while (e = document.getElementsByTagName ('SPAN')[i++]) {if (e.className == 'CLASSNAME') alert (e.firstChild.data)};

}

// -->

</script>

</head>

<body>

<p id="giant-says"><span class="CLASSNAME">Fee</span>, <span class="CLASSNAME">Fie</span>, <span class="CLASSNAME">Foe</span>, <span class="CLASSNAME">Fum</span>.</p>

</body>

</html>[/font]

And I should note, HTML is case insensitive but JavaScript is not. In HTML all element names are uppercase and all attribute names are lowercase. Most Browsers seem to do all right if you get this wrong when playing with the DOM, but let's not take any chances.
Copy linkTweet thisAlerts:
@CharlesDec 14.2004 — And yes, if you are trying to read some other, external file thn you cannot do that with JavaScript. Except that [i]some[/i] browsers have extended JavaScript in various ways that do allow you to allow you to do that. But you are better off finding a better way.

Unless you are using server side JavaScript. But you have given us so little to work with that it is difficult to helpful.
Copy linkTweet thisAlerts:
@burdenauthorDec 14.2004 — Thank you,

The idea was to read external files so I will find another way of getting there...

Sorry if I wasted your time.

burden
Copy linkTweet thisAlerts:
@scragarDec 14.2004 — reading a file is not imposible, although it is fairly complexe and often involves a frame. Oh and you can't read content from files hosted at a different site(so you cant read files hosted by google if your site is ninjapirate or something different)...
Copy linkTweet thisAlerts:
@pj59Dec 14.2004 — Hello!

If you have PHP available, you can use its' file() function to read a remote file and filter out the stuff you need.

Regards PJ
Copy linkTweet thisAlerts:
@CharlesDec 14.2004 — And the wonder of the PHP method is that it will actually work. Which is not something that one can say for JavaScript.
Copy linkTweet thisAlerts:
@scragarDec 14.2004 — yeah, I was wondering what the difference was, I thought it was just that JS requires about 100 more lines of code.
Copy linkTweet thisAlerts:
@CharlesDec 14.2004 — It's only a couple extra lines, but JavaScript itself only works nine times out of ten. That's one huge failure rate.
×

Success!

Help @burden spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...