Get text within tags or get class instead of Id

Discussion in 'Javascript' started by svintuss, Nov 2, 2005.

  1. svintuss

    svintuss Guest

    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?
     
    svintuss, Nov 2, 2005
    #1
    1. Advertising

  2. svintuss

    RobG Guest

    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;

    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
     
    RobG, Nov 3, 2005
    #2
    1. Advertising

  3. svintuss

    svintuss Guest

    Thanks a lot. It really helped me. Now I can get the contents of the
    article with el.innerText.
     
    svintuss, Nov 4, 2005
    #3
  4. svintuss

    svintuss Guest

    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.
     
    svintuss, Nov 4, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ranganath

    Custom Tags within Custom Tags.

    Ranganath, Oct 17, 2003, in forum: Java
    Replies:
    2
    Views:
    470
    Ranganath
    Oct 21, 2003
  2. Kwasi
    Replies:
    13
    Views:
    608
    Kwasi Yeboah via JavaKB.com
    Dec 1, 2004
  3. Chris

    table-tags within div-tags

    Chris, Feb 9, 2004, in forum: HTML
    Replies:
    5
    Views:
    13,310
    Chris
    Feb 10, 2004
  4. Donald Firesmith

    html tags within meta tags allowed?

    Donald Firesmith, Jan 5, 2005, in forum: XML
    Replies:
    5
    Views:
    918
    Andy Dingley
    Jan 8, 2005
  5. theintrepidfox
    Replies:
    0
    Views:
    286
    theintrepidfox
    Nov 26, 2007
Loading...

Share This Page