By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Get text within tags or get class instead of Id

P: n/a
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?

Nov 2 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
svintuss wrote:
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[i];

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;

--
Rob
Nov 3 '05 #2

P: n/a
Thanks a lot. It really helped me. Now I can get the contents of the
article with el.innerText.

Nov 4 '05 #3

P: n/a
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[i];

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.

Nov 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.