Finding the Host name on a particular window?

B

Bilal

Hi,
I have a web application that operates on several windows. Each window
is named win_1, win_2, win_3,... . When I quit a session, I usually
loop through all the windows and close one by one. So far so good
until my client wants to surf on 1 particular window and doesn't want
that window to close when a I quit a session.
I had something in mind:
find the window url through "window.location.host" or something (I'd
save it as a global variable) and when I loop through my windows to
quit one by one, I thought I'd compare their host and if it is not
equal, I leave it open. I'm using the following codes at the moment:
_________________________
for($i=2;$i<=$window_number;$i++) {
$str = "if (window.parent.name != 'win_'".$i."') {window.open
('','win_'".$i."').close();}";
echo $str;
}
.....
echo "if (window.parent.name != 'win_1'){window.parent.close();}";
_________________________

Suppose win_3 has url "www.google.com", how can I retrieve the host
name of win_3? Is it possible? Is there any easy way without having
recourse to major changes? I appreciate any suggestions.
Kind regards
Bils
 
R

Rico Huijbers

Bilal said:
Hi,
I have a web application that operates on several windows. Each window
is named win_1, win_2, win_3,... . When I quit a session, I usually
loop through all the windows and close one by one. So far so good
until my client wants to surf on 1 particular window and doesn't want
that window to close when a I quit a session.
I had something in mind:
find the window url through "window.location.host" or something (I'd
save it as a global variable) and when I loop through my windows to
quit one by one, I thought I'd compare their host and if it is not
equal, I leave it open. I'm using the following codes at the moment:
_________________________
for($i=2;$i<=$window_number;$i++) {
$str = "if (window.parent.name != 'win_'".$i."') {window.open
('','win_'".$i."').close();}";
echo $str;
}
....
echo "if (window.parent.name != 'win_1'){window.parent.close();}";
_________________________

Suppose win_3 has url "www.google.com", how can I retrieve the host
name of win_3? Is it possible? Is there any easy way without having
recourse to major changes? I appreciate any suggestions.
Kind regards
Bils

Well, window.location.href will give you the URL of the page that is
displayed in the given window. So I suppose you could substring search
it, e.g:

if (window.location.href.indexOf('www.myhost.com') >= 0) {
// ...close window...
}

But, without further knowing what you want to do, I find the notion of a
multi-window web application rather ill-conceived. Are you sure you need
multiple windows and if so, couldn't you just use frames?

-Rico
 
M

Matt Kruse

Bilal said:
Suppose win_3 has url "www.google.com", how can I retrieve the host
name of win_3? Is it possible?

No. Javascript security prohibits you from seeing anything about a window
which is not in the same domain as the one the code is running in. If you
try to get the href of a window which is at google.com, you'll get a
run-time error. You can trap this if you'd like, and continue on to the next
window.
 
R

Randy Webb

Bilal said:
Hi,
I have a web application that operates on several windows. Each window
is named win_1, win_2, win_3,... . When I quit a session, I usually
loop through all the windows and close one by one. So far so good
until my client wants to surf on 1 particular window and doesn't want
that window to close when a I quit a session.
I had something in mind:
find the window url through "window.location.host" or something (I'd
save it as a global variable) and when I loop through my windows to
quit one by one, I thought I'd compare their host and if it is not
equal, I leave it open. I'm using the following codes at the moment:
_________________________
for($i=2;$i<=$window_number;$i++) {
$str = "if (window.parent.name != 'win_'".$i."') {window.open
('','win_'".$i."').close();}";
echo $str;
}
.....
echo "if (window.parent.name != 'win_1'){window.parent.close();}";
_________________________

Suppose win_3 has url "www.google.com", how can I retrieve the host
name of win_3?

You can't, unles win_3's URL is in your domain. Its a cross domain
security issue.
Is it possible?

Fortunately, no.
Is there any easy way without having recourse to major changes?

Not really.
I appreciate any suggestions.

Determine why you need 3+ windows open for your app to work, and then
work backwards and make it work within one window.
 
R

Randy Webb

Rico Huijbers wrote:

Well, window.location.href will give you the URL of the page that is
displayed in the given window. So I suppose you could substring search
it, e.g:

if (window.location.href.indexOf('www.myhost.com') >= 0) {
// ...close window...
}

That will work, if and only if, its in the same domain as the page
calling it. Otherwise, its a cross-domain security issue.

But, location.hostname is what the OP's after.
 
D

Default User

Rico said:
But, without further knowing what you want to do, I find the notion of a
multi-window web application rather ill-conceived.

That's good advice. Most people don't like web sites opening up a bunch
of new windows.
Are you sure you need
multiple windows and if so, couldn't you just use frames?


Ugh. Frames are not the solution to anything. The application should
move naturally from content page to content page as needed.




Brian Rodenborn
 
M

Matt Kruse

Default said:
Ugh. Frames are not the solution to anything. The application should
move naturally from content page to content page as needed.

That's fine in most situations, but not always the best solution :)
 
C

Chung Leong

Randy Webb said:
You can't, unles win_3's URL is in your domain. Its a cross domain
security issue.

But for the same reason close() won't on that window either. Thus the stated
problem doesn't actually exist--not in Internet Explorer anyway.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top