"Dummy" JavaScript variable -- possible?

B

Bryan Ashby

All,

I'm looking for a way to define a "dummy" variable in JavaScript;
specifically for the window object. I would like to define a window
object that would normally be generated with window.open() but not
actually open a new window.

E.g.: normal code could look something like this:
aVar = window.open("http://www.someurl.com", ...);
aVar.location = "http://www.newlocation.com/";
document.write("made it here OK");

If I remove the aVar = window.open(... line then aVar.location will
fail and the rest of the script breaks - "made it here OK" will not be
printed, etc.

I would liek to do something like this:
vVar = create_dummy_window; // does not open a window
aVar.location = "http://www.newlocation.com/"; // this should now not
break & the rest of the script should now be OK.
document.write("made it here OK"); // should be printed this time

Is this possible? Any ideas here?

Thanks,

Bryan Ashby
 
E

Evertjan.

Bryan Ashby wrote on 25 nov 2003 in comp.lang.javascript:
I would liek to do something like this:
vVar = create_dummy_window; // does not open a window
aVar.location = "http://www.newlocation.com/"; // this should now not
break & the rest of the script should now be OK.
document.write("made it here OK"); // should be printed this time

Is this possible? Any ideas here?

Easy:

<script>
vVar = new window();
aVar.location = "http://www.newlocation.com/";
document.write("made it here OK");
</script>










No, not possible. ;-}
 
M

Michael Winter

Bryan Ashby wrote on 25 Nov 2003:

Is this possible? Any ideas here?

You could create an object that mimics the window object, but that
would be quite a task. Whether naming that object, 'window', is
allowable, and whether it would hide and override the original
object, are different questions altogether.

Mike
 
E

Evertjan.

Michael Winter wrote on 25 nov 2003 in comp.lang.javascript:
Bryan Ashby wrote on 25 Nov 2003:



You could create an object that mimics the window object, but that
would be quite a task. Whether naming that object, 'window', is
allowable, and whether it would hide and override the original
object, are different questions altogether.

You could name it "Window".
 
L

Lasse Reichstein Nielsen

I'm looking for a way to define a "dummy" variable in JavaScript;
specifically for the window object. I would like to define a window
object that would normally be generated with window.open() but not
actually open a new window.

Why a window object then. I.e., why not just any object.
E.g.: normal code could look something like this:
aVar = window.open("http://www.someurl.com", ...);
aVar.location = "http://www.newlocation.com/";
document.write("made it here OK");

If I remove the aVar = window.open(... line then aVar.location will
fail and the rest of the script breaks - "made it here OK" will not be
printed, etc.
Yes.

I would liek to do something like this:
vVar = create_dummy_window; // does not open a window

Try
var vVar = {};
It is equivalent to
var vVar = new Object();
aVar.location = "http://www.newlocation.com/"; // this should now not
break & the rest of the script should now be OK.

You can assign to the location property of any object. It's just in
windows objects that it does something.
document.write("made it here OK"); // should be printed this time

/L
 
R

Richard Cornford

You could name it "Window".

Gecko browsers have a global function property with the name "Window"
(the window constructor) and probably would not like having it replaced.
I don't like the suggestion of - vVar = new window(); - in your other
post much either as "window" is a reference to the current window object
and not a function, so conforming ECMA Script implementations should be
expected to throw a TypeError exception at that line.

It sounds to me like the OP wants a general (I assume that assigning to
the location is just an example, else Lasse's response fits the
requirement) window impersonating object that will act as a sink for any
subsequent window interaction. That probably cannot be done 100% but
these objects:-

function WindowDummy(url){
this.self = (this.window = (this.parent =
(this.frames = (this.top = this))));
this.opener = window;
this.open = window.open;
this.closed = false;
this.location = new LocationDummy(url);
this.document = new DocumentDummy(this.location);
this.length = 0;
this.focus = forDummys;
this.close = function(){this.closed = true;};
this.navigator = window.navigator;
this.resizeTo = (this.resizeBy = (this.moveBy =
(this.moveTo = forDummys)));
}
function LocationDummy(url){
this.href = url;
this.toString = function(){return this.href;};
this.refresh = (this.replace = forDummys);
}
function DocumentDummy(loc){
this.location = loc;
this.body = this.documentElement = {};
this.close = (this.open = (this.write = (this.writeln = forDummys)));
this.links = (this.anchors = (this.forms = (this.images = [])));
}
var forDummys = function(){return;};

-have a reasonable go at it (and could be added to).

Richard.
 
E

Evertjan.

Richard Cornford wrote on 25 nov 2003 in comp.lang.javascript:
I don't like the suggestion of - vVar = new window(); - in your other
post much either as

While it "felt" kind of good, that started as a joke.
this.self = (this.window = (this.parent =
(this.frames = (this.top = this))));

Do we need al those () ?
Parsing will bubble to the right anyway, IMHO ?

this.self=this.window=this.parent=this.frames=this.top=this;
 
B

Bryan Ashby

Lasse Reichstein Nielsen said:
Why a window object then. I.e., why not just any object.

I am writing a "JavaScript proxy" of sorts; preventing popups/etc. One
of the problems I have had is removing a variable and the rest of the
script breaks.
Try
var vVar = {};
It is equivalent to
var vVar = new Object();

This works great!

You can assign to the location property of any object. It's just in
windows objects that it does something.

That's fine. The only thing I'm looking for here is to be able to
remove (well actually replace a variable line) (e.g.: aVar =
window.open(...) -> var aVar = {};) and not have the rest of the
script "terminate"

Bryan
 
R

Richard Cornford

Evertjan. said:
While it "felt" kind of good, that started as a joke.

Sorry, when I scrolled your post down to the large blank space I assumed
that I had got to the end of it, if I had kept going I would have seen
the indicator that it was humorous.
Do we need al those () ?
Parsing will bubble to the right anyway, IMHO ?

this.self=this.window=this.parent=this.frames=this.top=this;

They are not needed, but neither are LF/CR pairs to separate statements
or tabs and spaces to indent blocks. Some things get into source code
for the benefit of human readers instead of machine interpreters.

Richard.
 
R

Richard Cornford

I am writing a "JavaScript proxy" of sorts; preventing popups/etc.
One of the problems I have had is removing a variable and the rest
of the script breaks.

I guessed that this was your plan. You should have a look at a recent
thread with the subject "Hi, stupid popup question" (2003-11-12) because
Yann-Erwan Perio has demonstrated that proxy based pop-up blocking can
be entirely circumvented. Which might be grounds to reconsider putting
any effort into writing one if pop-up blocking is the main intention.
This works great!

It would work fine with the simple case of assigning a value to the
location property of the new window. But don't you need a more general
solution to deal with other common cross-window interaction scripts? It
is fairly common, for example, for scripts to assign values to the
location.href property (though not recommended over assuaging to the
location property). Calling the resizeTo and moveTo method of the new
window is also common (also not recommended) as is writing contents into
new windows with - vVar.document.write(" ... ") -.
That's fine. The only thing I'm looking for here is to be able to
remove (well actually replace a variable line) (e.g.: aVar =
window.open(...) -> var aVar = {};) and not have the rest of the
script "terminate"

And:-

var vVar = window['open'](...);
-or-
var vVar = window['op'+'en'](...);
-or-
var x = 'open';
var vVar = this[x](...);
-or-
var document = window;
var vVar = document.open(...);

- or any other of the many possible methods of calling the window.open
function without needing to write the string "window.open" onto the
source code? Unfortunately, the people who are most likely to obscure
the window.open call in the code are also the people who's pop-ups most
deserve to be blocked.

Richard.
 

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

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top