Get text within tags or get class instead of Id

S

svintuss

Hi. I'm trying to create a widget for Tiger, which should use a
web-dictionary. I want the widget to display translation within its
window, so it has to copy all the data within the tags <span
class="lingvo-article"> </span>.
Is there a method like document.getElementById to get the class
contents?
Or it's only possible to search for opening and ending tags, then copy
the text inside?
 
R

RobG

svintuss said:
Hi. I'm trying to create a widget for Tiger, which should use a
web-dictionary. I want the widget to display translation within its
window, so it has to copy all the data within the tags <span
class="lingvo-article"> </span>.
Is there a method like document.getElementById to get the class
contents?
Or it's only possible to search for opening and ending tags, then copy
the text inside?

I haven't written any Dashboard widgets, perhaps there's a better forum
for that. There's some good stuff here:

<URL:http://developer.apple.com/macosx/dashboard.html>

It seems to be pretty much the same as typical web stuff, so you should
be able to get the following working.

There is no 'getElementsByClassName' method, though it is asked for
reasonably often. You have to sift through the document looking for
matches. If you are only looking for span elements, then:


var docBody = document.body || document.documentElement;
if (!docBody || !docBody.getElementsByTagName) return;

var el;
var spans = docBody.getElementsByTagName('span');
var classSpan = [];
var classMatch = /\blingvo-article\b/;

for (var i=0, len=spans.length; i<len; ++i){
el = spans;

if (el.className && classMatch.test(el.className)){
// el is a 'lingvo-article' span,
// do something with it
}
}

If you are looking for all elements and not just spans, you'll need to
replace:

var spans = docBody.getElementsByTagName('span');


with something like:

var allElems = document.getElementsByTagName("*");

Presumably this will only ever run on Mac OS X 10.4 or higher, so
feature detection and fall-back is not really required.

I've recently posted stuff on getting the text content of a cell, search
the archive for 'textContent innerText' and sort by date. If Tiger
supports textContent (it's in DOM 3 so maybe not but Firefox has it),
you can get the element's text content quite simply:

var theContent = el.textContent;
 
S

svintuss

Thanks a lot. It really helped me. Now I can get the contents of the
article with el.innerText.
 
S

svintuss

I'm now curious about how to make the function with the text above
process the input, obtained via XMLHttpRequest():

function getArticle(event)
{
var url = "http://lingvo.yandex.ru/en?text=test";
req = new XMLHttpRequest();
req.open("GET", url, false);
req.send(null);
response = req.responseXML; //or req.responseText;
processHTML(response);
}

function processHTML(input)
{
alert(input); //if used responseText it returns raw text of the
page, if responseXML - returns "undefined"
var docBody = document.body || document.documentElement;
if (!docBody || !docBody.getElementsByTagName) return;

var el;
var spans = docBody.getElementsByTagName('span');
var classSpan = [];
var classMatch = /\blingvo-article\b/;
var text = '';

for (var i=0, len=spans.length; i<len; ++i){
el = spans;

if (el.className && classMatch.test(el.className)){
text += el.innerText;
}
}

alert(text); //returns nothing in both cases

}

Seems to me, that the processHTML() function works with my page, not
with the one downloaded from the net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top