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

"Dummy" JavaScript variable -- possible?

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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. ;-}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #2

P: n/a
Bryan Ashby wrote on 25 Nov 2003:

<snipped description>
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

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #3

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

<snipped description>
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.


You could name it "Window".
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #4

P: n/a
br********@hotmail.com (Bryan Ashby) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
<snip>
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".


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.
Jul 20 '05 #6

P: n/a
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;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #7

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<ek**********@hotpop.com>...
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
Jul 20 '05 #8

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
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.


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.
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=thi s.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.
Jul 20 '05 #9

P: n/a
"Bryan Ashby" <br********@hotmail.com> wrote in message
news:17**************************@posting.google.c om...
<snip>
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.
Try
var vVar = {};
It is equivalent to
var vVar = new Object();


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(" ... ") -.
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"


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.
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.