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

Creating a new document object?

P: n/a
Working on a google homepage module. One of the things I do is
retrieve results from a search into a string. I want to be able to
access all the anchors in this string, (their name, href, and
innerHTML) and using regex for string parsing would be tiresome and
difficult for this.

I've searched far and wide, but I can't seem to find out if its
possible to create a new document object (that doesnt refer to the
current document) with a string of text (or the address of the search
result, doesn't matter to me.)

That way, with this new document object, I can simply use the .anchors
attribute for all my needs.

Is this possible?

Dec 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


al**********@gmail.com wrote:
One of the things I do is
retrieve results from a search into a string. I want to be able to
access all the anchors in this string, (their name, href, and
innerHTML) and using regex for string parsing would be tiresome and
difficult for this.

I've searched far and wide, but I can't seem to find out if its
possible to create a new document object (that doesnt refer to the
current document) with a string of text

Browser allow you to load a URL in a window or frame and depending on
the same origin policy you can script the contents once it has been loaded.
Pure HTML parsing of complete HTML document markup in a string is
usually not exposed directly as a method, of course there is the good old
frameOrWin.document.open();
frameOrWin.document.write(stringWithHTMLMarkup);
frameOrWin.document.close();
// now access e.g
frameOrWin.document.links

If you have only snippets of HTML markup that would fit into a div then
nowadays you can make use of innerHTML e.g.
var div = document.createElement('div');
div.innerHTML = stringWithHTMLSnippet;
// now access e.g.
div.getElementsByTagName('a')
But of course relative URLs in href attributes will be resolved with the
base URL of the ownerDocument of that div you created.

By now there is also DOMParser and its parseFromString method in
Mozilla, in Opera 8 and later, and I think in Safari 2.01.
But that method in Mozilla takes the content type as the second argument
and in Mozilla only supports XML content types like text/xml or
application/xml or application/xhtml+xml and throws an exception if
text/html is passed in.
Opera seems to happily accept text/html as the content type argument but
I don't get anything parsed according to HTML rules, it looks like the
XML parser is used and that content type argument is ignored.
Not sure what Safari 2.01 does, perhaps someone else can report.

Thus if you have the markup of an XHTML document in a string you can do e.g.

var xmlDocument = new DOMParser().parseFromString([
'<html xmlns="http://www.w3.org/1999/xhtml">',
' <head>',
' <title>Example</title>',
' </head>',
' <body>',
' <p>Kibology for all.</p>',
' <p>All for Kibology.</p>',
' </body>',
'</html>'
].join('\r\n'), 'application/xhtml+xml');
var paragraphs =
xmlDocument.getElementsByTagNameNS(xmlDocument.doc umentElement.namespaceURI,
'p');
alert('Found ' + paragraphs.length + ' paragraph elements.');

but only the Core DOM is available to access elements.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.