Why can't I construct an XMLHttpRequest?

  • Thread starter Chris Shearer Cooper
  • Start date
C

Chris Shearer Cooper

I'm writing some JavaScript code that is being called inside a third
party tool, and I have no idea what the context is, except that I'm
definitely not in a window (so things like setTimeout() can't be
used).

What I'm finding is that when I try to construct an XMLHttpRequest
object, it throws an "object error" exception.

Does XMLHttpRequest depend on being inside a window?

Do I need to construct it differently if I'm _not_ inside a window -
something like
var v = new window.XMLHttpRequest;

Or something weird like that?

Thanks,
Chris
 
T

Thomas 'PointedEars' Lahn

Chris said:
I'm writing some JavaScript code that is being called inside a third
party tool, and I have no idea what the context is, except that I'm
definitely not in a window (so things like setTimeout() can't be
used).

What I'm finding is that when I try to construct an XMLHttpRequest
object, it throws an "object error" exception.

How are you trying to do this? What is the "third party tool" you are
talking about? Doesn't the error message say more than just "object error"?

Does XMLHttpRequest depend on being inside a window?

The availability of the `XMLHttpRequest' identifier depends on an API that
provides it. Apparently this one does not. Possibility: the "third party
tool" uses MSHTML, version 6 or below.
Do I need to construct it differently if I'm _not_ inside a window -
something like
var v = new window.XMLHttpRequest;

Or something weird like that?

You need to think before you post. How can `window' possibly work if your
script runs, by your own account, "definitely not in a (browser?) window"?


PointedEars
 
T

Thomas 'PointedEars' Lahn

Danny said:
have you tried eval()?

eval() will not help here. If an identifier cannot be resolved, or a method
not called, eval() does not change that; what eval() can do is to hide
*syntax* (like try-catch-finally) from incompatible parsers.
I think it's always global;

You are mistaken. While eval() is a method of the ECMAScript Global Object,
the argument of eval(), if a string value, is evaluated as a /Program/ in
the execution context in which eval() was called:

var bar = "baz";

function foo(bar)
{
window.alert(eval("bar"));
}

// displays 42, not "baz"
foo(42);

Cf. the ECMAScript Language Specification, sections 13.2.1 and 15.1.2.1.

See also: <http://jibbering.com/faq/#eval>


PointedEars
 
M

Michael J. Ryan

I'm writing some JavaScript code that is being called inside a third
party tool, and I have no idea what the context is, except that I'm
definitely not in a window (so things like setTimeout() can't be
used).

What I'm finding is that when I try to construct an XMLHttpRequest
object, it throws an "object error" exception.

Does XMLHttpRequest depend on being inside a window?

Yes. XmlHttpRequest is a browser DOM object, not a part of the JavaScript
language... How you need to pull remote content depends on the evironment you
are running in, if it isn't a web browser, or that browser doesn't have
XmlHttpRequest, or ActiveXObject(IE), you need to know where you are running.
Do I need to construct it differently if I'm _not_ inside a window -
something like
var v = new window.XMLHttpRequest;

Or something weird like that?

You can try it, it could be that XmlHttpRequest doesn't exist in that
context/browser. Since window == global in the browser context...
 
M

Michael J. Ryan

What I'm finding is that when I try to construct an XMLHttpRequest
object, it throws an "object error" exception.

// Provide the XMLHttpRequest class for IE 5.x-6.x:
// Other browsers (including IE 7.x-8.x) ignore this when XMLHttpRequest is
predefined
if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
throw new Error( "This browser does not support XMLHttpRequest." )
};
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,098
Messages
2,570,625
Members
47,236
Latest member
EverestNero

Latest Threads

Top