473,597 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this a useful solution to fetching a window object by name?

Hi,

Having searched for a way to fetch a window object by name, all I came
across were answers along the line of...
"All you have to do is say windowObj = window.open("bl ah", "name");"
which isn't very useful if you want to fetch information from an
existing document in the window.

One solution I came up with was quite simple; open the "new" window,
giving you a reference to the object, then use window.history. back() to
force display of the previous contents. This isn't a perfect solution,
as it has the same effect as replacing the document, then clicking the
back button; but it seems to work.

The basic code required is given below. However, for those who are
interested, I've set up a working demonstration at
http://www.multicherry.com/windowbyname

*** FUNCTION:-

function get_window_obje ct_by_name(name Str)
{
var result = window.open('go back.htm', nameStr);
return result;
}

*** REQUIRED HTML DOCUMENT "goback.htm ":-

.....<body>
<script lang="javascrip t" type="text/javascript">
window.history. back();
</script>
</body>....

Any thoughts?

- MCh

(This message posted to the public comp.lang.javas cript Usenet
newsgroup via Google Groups. I am not associated with, nor a member of
any website/discussion-board which repackages and misrepresents such
discussions as belonging to themselves.)

Aug 19 '06 #1
7 1965
Ressources are not saved between page transition. Thereof, any declared
window object will be lost when you replace the page content with other
date (leaving the other--popup--window opened). There is a few dirty
"hacks" to accomplish what you're trying to do, but keeping an active
link between two window objects is not in the scope the the Javascript
language.

I didn't test this, but maybe it's a start :

// put this in a js file that you will include in all you pages.
function setWindowName( name ) {
window._windowN ame = name;
}

function createLinkedWin dow( url, name, options ) {
if ( !window._linked ) window._linked = {};

window._linked[name] = window.open( url, name, options );
window._linked[name]._linked[window._windowN ame] = window;
}

function changeWindowURL ( url, name ) {
window._linked[name]._linked[window._windowN ame].location.href = url;
window._linked[name]._linked[window._windowN ame] = window;
}

function getWindowByName ( name ) {
return window._linked[name]._linked[name];
}
Add this into a <scriptsectio n :

setWindowName( "MainWindow " ); // you should not use spaces in the
names
// create a new Window
createLinkedWin dow( 'www.domain.com/some/page.html', 'AnotherWindow' );
Then, call this function whenever you're about to load a different
page, for example :

<a href="#" onclick="change WindowURL(
'www.domain.com/another/page.html', 'AnotherWIndow' );" />Use Another
window to display another/page.html !</a>
Well... this is most likely to have a memory leak at some point, it
could be a start...

Aug 20 '06 #2
Yanick wrote:
Ressources are not saved between page transition.
Does "resource" have a specific (i.e. formal) meaning here? It's clear
that *some* information can be retained, but not (apparently) variables
and fields.
>Thereof, any declared
window object will be lost when you replace the page content with other
date (leaving the other--popup--window opened).
I don't understand; my demo shows that we *can* get the window object
(or one that is equivalent) and retrieve the *modified* HTML within the
old popup document. (That is, the old document retains any *changes*
made to its structure instead of just reloading "popup.htm" ).

Perhaps we're using the word "object" in slightly different ways. The
general problem I had (as I see it) is that when "second.htm " replaces
"first.htm" in the main window, we cannot transfer a *reference* to the
object.

Perhaps I'm incorrectly applying a Java-centric view of objects to
Javascript, but I'm assuming there's a single underlying window object
that we can have more than one reference to.
There is a few dirty
"hacks" to accomplish what you're trying to do,
Absolutely; my technique belongs to the "informal and messy" category
:)
I didn't test this, but maybe it's a start :
Your technique is interesting; I see what you're trying to do. I
haven't tried it out myself either (sorry :) ), but I see some
potential pitfalls:-

FIRST: The opened window only knows about the opening window by name.
It doesn't know about other windows its parent has opened (before OR
since the current window was created). And parents only "know" about
their children, *not* grandchildren, etc. (i.e. we have a tree-like
structure)

So although it's possible to find every window by name, we (probably)
have to find the tree root, then traverse some (or *all*) of the
branches to find the entry.

One solution might be to modify your code to either (a) Point to a
shared array containing all window names, or if that's not possible
with Javascript, (b) Copy the contents of the parent window's name
array.

Problem with (b) is that you then have to make sure that all the arrays
are kept up to date.

SECOND problem:
Here:-
window._linked[name] = window.open( url, name, options );
window._linked[name]._linked[window._windowN ame] = window;

I've got a horrible feeling that this may run into problems because the
new window won't be fully loaded when the second statement executes. I
might be wrong, but don't we have to wait for the window to load? How
do we do that synchronously?

Oh, apparently we can't.

(Disclaimer: I am *not* a Javascript expert. If I'm wrong, please
correct me, but I haven't come across solutions to the following
problems so far).

Sure, we can callback asynchronously via onload and friends. The major
problem (and one thing I really hate about Javascript) is that there
are times when you *don't want* to do things asynchronously; you'd like
to wait for the page to load.

AFAIK, it's not even possible to loop, doing a periodic check on some
attribute in the (new) page until it has loaded. Why? Because JS
doesn't support sleep(), so we can't put a reasonable gap between
checks. But a busy loop (e.g. while (! childWindow.che ck) { /* empty
loop */} ) has horrific effects on the browser performance (almost
locks it up).

I came up with a rather clever (I thought) solution to sleep() once. It
was simple; psleep() created and sent a *synchronous* XMLHttpRequest
for a "sleep.php" document, with the requested delay specified via
arguments. The document included some PHP code which caused the
*server* to sleep() for the requested number of seconds before
returning an empty (or irrelevant) document. The client, which was
waiting synchronously, was also delayed. Smart idea.

It didn't work.

Well, not under Firefox at any rate. The whole browser locked up for x
seconds, which wasn't the intention. Clearly, my best-guess mental
representation of Firefox's threading was faulty. Pity.

Why the *#$^ doesn't Javascript support sleep()? Seriously, there must
be a good reason for it, because it's a major PITA. Yes, I *know* about
setting timers/timeouts, etc., but they are *not* replacements for
sleep(); they have the same problems as described above.

Anyway, we now return you to your usual programming (no pun intended,
*cough*):-
Well... this is most likely to have a memory leak at some point, it
could be a start...
Leak? I hope not. AFAIK there's no manual malloc() in JS, so a true
leak would indicate a bug in the underlying implementation. Even valid
memory usage isn't likely to be that big for any app with a usable
number of windows...

- MCh

Aug 21 '06 #3
mu*********@lyc os.com wrote:
I came up with a rather clever (I thought) solution to sleep() once.
It was simple; psleep() created and sent a *synchronous*
XMLHttpRequest for a "sleep.php" document, with the requested delay
specified via arguments. ... Smart idea.
Horrible idea. Using synch mode for "ajax" requests is not a good practice
for the very reason you found - it locks up the entire browser and in some
cases parts of the OS until the response is received.
Why the *#$^ doesn't Javascript support sleep()?
Because there's isn't a need for it.
Javascript is single-threaded, and any activity that puts the entire browser
execution in a hold pattern for any amount of time is a bad idea.
Event-driven UI's don't like to work like that.
Leak? I hope not. AFAIK there's no manual malloc() in JS, so a true
leak would indicate a bug in the underlying implementation. Even valid
memory usage isn't likely to be that big for any app with a usable
number of windows...
It certainly is possible to create memory leaks using javascript in a
browser. Read up on it, and why MS doesn't consider it a bug in the
underlying implentation, or even worth fixing.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 21 '06 #4
Matt Kruse wrote:
mu*********@lyc os.com wrote:
I came up with a rather clever (I thought) solution to sleep() once.
It was simple; psleep() created and sent a *synchronous*
XMLHttpRequest for a "sleep.php" document, with the requested delay
specified via arguments. ... Smart idea.

Horrible idea.
Well, obviously. It was a reflection of what I was thinking at the
time; as I made clear, it failed horribly in practice.

But it seemed like a simple and ingenious solution at the time; and
quite frankly, unless you have a clear idea of how JS (or at least
Firefox) operates thread-wise, the flaws aren't obvious until you try
it out.
Using synch mode for "ajax" requests is not a good practice
for the very reason you found - it locks up the entire browser and in some
cases parts of the OS until the response is received.
Locking up the OS? That sounds like.... it really *shouldn't* be
allowed then.
Why the *#$^ doesn't Javascript support sleep()?

Because there's isn't a need for it.
Javascript is single-threaded, and any activity that puts the entire browser
execution in a hold pattern for any amount of time is a bad idea.
Event-driven UI's don't like to work like that.
Are you talking about "threads" within Javascript or within the
underlying OS/browser-implementation-language?

Are you saying that (from the JS developer's POV) there's only a single
thread, and (by implication) that events must wait in a queue until the
code handling the previous events finished executing?

Or put another way, does synchronous "waiting" for (e.g.) an onload to
be triggered therefore cause the onload-handling-function to never be
called?

We're also getting into the sticky realm of what is covered by
Javascript and what isn't. Experiments I carried out whilst trying to
do a non-busy wait after a window.open() seemed to work.

However (IIRC) I checked the page load by having the parent window
script check the existence of a particular attribute/method within the
child page, rather than have the child calling back via onload. So this
doesn't necessarily contradict what you said; since page loading isn't
part of JS and thus shouldn't need another JS thread running.

Anyway, the truth is that these ideas (get_window_by_ name() and
sleep()) were workarounds for problems in the architecture of a system
designed when I knew very little Javascript (and later had to abandon).
Had I known what I knew even six months later, it would have been
completely different.
Leak? I hope not. AFAIK there's no manual malloc() in JS, so a true
leak would indicate a bug in the underlying implementation. Even valid
memory usage isn't likely to be that big for any app with a usable
number of windows...

It certainly is possible to create memory leaks using javascript in a
browser. Read up on it, and why MS doesn't consider it a bug in the
underlying implentation, or even worth fixing.
I will, and I'll take your word for that now, but it still sounds like
a "bug" to me, regardless of what MS say. :-)

- MCh

Aug 21 '06 #5
mu*********@lyc os.com said the following on 8/21/2006 6:50 AM:
Matt Kruse wrote:
<snip>
>Using synch mode for "ajax" requests is not a good practice
for the very reason you found - it locks up the entire browser and in some
cases parts of the OS until the response is received.

Locking up the OS? That sounds like.... it really *shouldn't* be
allowed then.
>>Why the *#$^ doesn't Javascript support sleep()?
Because there's isn't a need for it.
Javascript is single-threaded, and any activity that puts the entire browser
execution in a hold pattern for any amount of time is a bad idea.
Event-driven UI's don't like to work like that.

Are you talking about "threads" within Javascript or within the
underlying OS/browser-implementation-language?
Javascript as it doesn't have multiple threads. It has single threads
and nothing more.
Are you saying that (from the JS developer's POV) there's only a single
thread, and (by implication) that events must wait in a queue until the
code handling the previous events finished executing?
That is precisely what happens.
Or put another way, does synchronous "waiting" for (e.g.) an onload to
be triggered therefore cause the onload-handling-function to never be
called?
Probably so. Test it :)

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 21 '06 #6
mu*********@lyc os.com wrote:
Yanick wrote:
Ressources are not saved between page transition.

Does "resource" have a specific (i.e. formal) meaning here? It's clear
that *some* information can be retained, but not (apparently) variables
and fields.
Thereof, any declared
window object will be lost when you replace the page content with other
date (leaving the other--popup--window opened).
What I meant by that is that any variables, objects, functions, etc.
declared (or in other words, any memory allocated by the browser for
any javascript ressource) will be cleared after the onunload event is
fired, before the new page begins to be downloaded. Thus, preserving
any value between pages is impossible without the use of cookies or
some kind of synchronisation ...

You were right about the potential problems in my code (what was I
thinking ?). Synchronizing a tree of window object's references is a
good idea. But that will always have a fall back because, as you said,
knowing when the browser is done loading data is not an easy task
(especially when there's a lot of ressources to load).

Memory leaks with Javascript are easier to produce than you might
think... Here's a link that gives some explanations :
http://www.codeproject.com/jscript/LeakPatterns.asp .

Playing with server side session IDs (with the help of
XMLHttpRequests ...) you could synchronize all your "registered " windows
with a server side script... Or use a frameset with a single frame
(top) to load all you different pages and keep the Javascript
environment variables in the window scope (a messy solution...).
There's no straight foreward solutions, and the language is limited in
this situation (cookies cannot store objects), you will soon find that
memory leakage and broken references (or even cyclic ones) is more an
eventuality than possibility.

Personally, I'd recommand that you find other solutions to retreive
data from child windows ; other solutions than keeping them in sync.
But I guess that, if weren't busy with all sort of things, I myself
would try to make this work.. .just for the heck of it. :)

Anyhow, good luck.

Aug 23 '06 #7
Yanick wrote:
You were right about the potential problems in my code (what was I
thinking ?).
Not a problem; you pretty much said yourself that it was only a quick,
rough and untested idea, designed to illustrate a principle and nothing
more. So it wasn't my intention to hold it to the same standards as
(e.g.) the Linux kernel.... I was just proving I'd actually read it :-)
Memory leaks with Javascript are easier to produce than you might
think... Here's a link that gives some explanations :
http://www.codeproject.com/jscript/LeakPatterns.asp .
Haven't had time to consider everything in the article in depth, but it
appears that one of the examples is a circular reference being missed
because it straddles the territory of the DOM garbage collector and the
Javascript one. Interesting...
Playing with server side session IDs (with the help of
XMLHttpRequests ...) you could synchronize all your "registered " windows
with a server side script... Or use a frameset with a single frame
(top) to load all you different pages and keep the Javascript
environment variables in the window scope (a messy solution...).
That's the one I was thinking of, and the way I would have done it in
the first place (or at least I'd have used frames), had I not been a
complete JS newbie when I designed the code and been unable to easily
change it later.
There's no straight foreward solutions, and the language is limited in
this situation (cookies cannot store objects), you will soon find that
memory leakage and broken references (or even cyclic ones) is more an
eventuality than possibility.
I have to say that I'm always asking myself how much my frustration
with JS is down to lack of experience and how much is down to
limitations with the language. In the case of JS, it's far more
advanced than it used to be, but I'm still aware that it's a simple
scripting language with stuff added on later... not to mention issues
of compatibility and incompatibility with different browsers and older
code.

Anyway, thanks to everyone for the feedback.

Bottom line, my window-grabbing "tip" still works (if you can live with
its hackiness), but the limitations described above mean its usefulness
isn't that great for most situations. :-/
- MCh

Aug 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1651
by: Paul Eden | last post by:
Afternoon all. I wonder if someone might lend a little hand here? I'm using input.htm to take some data using radio and a text field from the user, which then "get"s them to update.php. Now then, there is a lot of data to input, so i need to go back to input.htm repeatedly, so, at the bottom of update.php, there is an image which has a javascript onload command, that loads up update.php. You probably know where this is going by now?...
26
3128
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I had done myself a disservice by having not attempted and completed the exercises. I intend to rectify that. My current routine is to read a chapter and then attempt every exercise problem, and I find that this process is leading to a greater...
7
2169
by: Brad | last post by:
When debugging my current web project, in VS2003, I found I had lost the ability to drill down on watch objects in the Watch Window; I could only view the single value specific watch objects. Here's what I discovered. In addition to my main web project and several middle tier projects, I also added an "empty" web project in my solution (Add - New Project - Empty Web Project). This empty web project is what is causing the above...
1
11559
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at http://weblogs.asp.net/asmith/archive/2003/09/15/27684.aspx but it was far more complex then I needed. (I got lost trying to figure it all out). Therefore, here goes my simple "web dialog box with parent event handler fireing" solution. ...
5
7445
by: Ram | last post by:
Hi Friends I want to develope a custom control in .net which can be used with any project. I am writing a function in that class which I want to take any object as parameter. For that I have used Object class as parameter. Now it can take any object as its parameter. But the problem is that I want to access the values of the private or public member variables of the passed object, for which I may have to typecast the Object class...
26
5660
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized several parts of the DOM, but this does not include the window object. Thank you
1
2271
by: nasirmajor | last post by:
Dear All, im fetching record from database using datareader, and is displaying them in the textboxes. when user change the text fields and then presses the update button the record should be updated but what happen is: form is postedback but no change happen to database and also textboxes return to there original state i.e previous record(enableviewstate is false/same result with true). small code is here:...
69
5555
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
24
8202
by: Jeremy J Starcher | last post by:
While reading c.l.j, I've noticed that some people prefer and indeed even recommend the use of "window.alert()" over "alert()". I can't find any technical reason to make this distinction, and seems to have a (tiny) amount overhead since window itself points to the global object, hence, a circular reference. (From everything I am reading, window is just a REFERENCE back to the global object, as opposed to a separate object.)
0
7977
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8044
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8261
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
1496
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.