/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] A prototype on XMLDocuments?

Take a look at the snippet below. It pertains to XMLHttpRequest objects.

httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == XHR_SUCCESS && httpRequest.state == HTTP_SUCCESS)
return httpRequest.responseXML.asObject();
}

You’ll notice that there’s a prototype hung off of responseXML: asObject(). If I wanted to defined that prototype that’s supported cross-browser, how would I? Can you fill in the blank for me?

______.prototype.asObject = function() {
// JSON.fromDOMtoObject() converts an
// XML document into a JSON string,
// then eval()s the string and returns
// the Object that eval() created. This
// function is already defined in my
// framework.
return JSON.fromDOMtoObject(this);
}

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@ChrisOauthorJan 12.2008 — I believe this is the answer. I've just read that one can add prototypes to host objects in addition to native objects.

[CODE]// Add asObject() to the browser's XML document object
{
if (window.DOMParser)
addPrototype(window.DOMParser);
else if (window.ActiveXObject)
addPrototype(window.ActiveXObject);

function addPrototype(XMLDocument) {
XMLDocument.prototype.asObject = function() {
return JSON.fromDOMtoObject(this);
}
}
}[/CODE]
×

Success!

Help @ChrisO spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...