473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Creating a new document object?

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
1 3405


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kerri McDonald | last post by:
We have an application where the user fills out many screens and when they are done, we are supposed to display the text they entered in a word or excel format. That is fairly easily accomplished...
22
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
0
by: volume | last post by:
Hi all, In an effort to try to impress the boss, I would like to log accounting information to an Excel spreadsheet using C# and .NET. This would be a mockup only, not a real solution - yet! We...
0
by: Phani | last post by:
Hi All, I have 3 questions here: 1) Problem creating a SPFolder object: I am trying to create a SPFolder object for a document/picture library using the following code with SOM
4
by: deadlyicon | last post by:
So I wanted an object to have a reference to a string in side of an array some where else and so here is how I made that possible: <div id="something"> </div> <script> var obj =...
9
by: William | last post by:
Heya, I have been looking all over Google for ways to *create* an XML document using any Javascript object, and it seems that no one has ever tried or ever needed this. I want to send the...
2
by: Moses | last post by:
Hi All, Is is possible to catch the error of an undefined element while creating an object for it. Consider we are not having an element with id indicator but we are trying to make the object...
0
by: PracticalApps | last post by:
I looked to find a canned solution to create a Word document in my application and just couldn't find anything that just gets to the point. I would think, and I may be making too strong of an...
6
by: cleary1981 | last post by:
I have adapted code from http://dunnbypaul.net/js_mouse/ I want to use a button to create new draggable divs but i keep getting error "is null or not an object" heres the code <html> <head>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.