www.webdeveloper.com
Results 1 to 2 of 2

Thread: Select arbitrary object using XPath

  1. #1
    Join Date
    Oct 2007
    Posts
    5

    Select arbitrary object using XPath

    I want to select a specific <font> tag on a page using javascript, but it's buried within the code and has no id or class or anything associated with it. I know you can use XPath to select it but everything I tried is not functional.

    For a simple example, go to http://www.google.com/ and try selecting the tiny text that says "2009 - Privacy"
    I found the XPath, but don't know how to use it in javascript: /html/body/center/p/font

    Thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Posts
    96
    With Firefox and any reasonably modern JavaScript-enabled browser other than Internet Explorer, you can use the following:
    Code:
    var xpathExpr = ;
    var xpathResult = document.evaluate(xpathExpr, document, null, XPathResult.ANY_TYPE, null);
    alert(xpathResult.iterateNext());
    As for Internet Explorer, I have yet to figure out how to use it through JavaScript (MSDN documentation uses the .NET library ). However, there is a library that provides XPath support in HTML. Check it out. Unfortunately, it is no longer under development because the author feels it might not be a necessity to have XPath support in HTML.

    You might also try using DOM methods instead, though success is most definitely limited. For example:
    Code:
    var fontEltsEnum = new Enumerator(document.getElementsByTagName('font'));
    var fontElts = new Array();
    while (!fontEltsEnum.atEnd()) {
        var node = fontEltsEnum.item();
        if ((node.parentNode.nodeName.toLowerCase() == 'p')
            && (node.parentNode.parentNode.nodeName.toLowerCase() == 'center')
            && (node.parentNode.parentNode.parentNode.nodeName.toLowerCase() == 'body')) {
            fontElts.push(node);
        }
        fontEltsEnum.moveNext();
    }
    Whatever the 'fontElts' variable contains should be effectively the same as what you would get using XPath. The DOM stuff matches the XPath, but Google is rendered in quirks mode in IE, which means the 'p' element referenced isn't even generated. As a result, the 'p' element is never detected, and the target element(s) is/are never found.

    Another option would be to try loading the page as XML by creating a new Msxml2.DOMDocument ActiveX object. There is only one problem - Google uses HTML, which means it won't work. Simply put, it would take a fair bit of work in IE...even IE8.

    Simply put, even though DOM methods and properties are widely supported across various browsers, they don't help when browsers don't generate the same markup!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles