Finding the Host name on a particular window?

Discussion in 'Javascript' started by Bilal, May 24, 2004.

  1. Bilal

    Bilal Guest

    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
     
    Bilal, May 24, 2004
    #1
    1. Advertising

  2. Bilal wrote:
    > 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
     
    Rico Huijbers, May 24, 2004
    #2
    1. Advertising

  3. Bilal

    Matt Kruse Guest

    Bilal wrote:
    > 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.

    --
    Matt Kruse
    Javascript Toolbox: http://www.mattkruse.com/javascript/
     
    Matt Kruse, May 24, 2004
    #3
  4. Bilal

    Randy Webb Guest

    Bilal wrote:
    > 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.


    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/
     
    Randy Webb, May 24, 2004
    #4
  5. Bilal

    Randy Webb Guest

    Rico Huijbers wrote:

    <--snip-->

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




    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/
     
    Randy Webb, May 24, 2004
    #5
  6. Bilal

    Default User Guest

    Rico Huijbers wrote:
    >
    > Bilal wrote:
    > > 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.



    > 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
     
    Default User, May 24, 2004
    #6
  7. Bilal

    Matt Kruse Guest

    Default User wrote:
    > 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 :)

    --
    Matt Kruse
    Javascript Toolbox: http://www.mattkruse.com/javascript/
     
    Matt Kruse, May 24, 2004
    #7
  8. Bilal

    Chung Leong Guest

    "Randy Webb" <> wrote in message
    news:...
    > Bilal wrote:
    > > 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.


    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.
     
    Chung Leong, May 24, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey
    Replies:
    0
    Views:
    351
  2. Orpheus66
    Replies:
    0
    Views:
    4,442
    Orpheus66
    Jul 30, 2003
  3. Anil G
    Replies:
    4
    Views:
    22,896
    v_parthi
    Jan 31, 2008
  4. Jerry Qu
    Replies:
    1
    Views:
    494
    lee atkinson
    Feb 20, 2009
  5. Anukul Singhal

    Finding Process Name using Window Handle

    Anukul Singhal, Mar 9, 2009, in forum: Ruby
    Replies:
    0
    Views:
    128
    Anukul Singhal
    Mar 9, 2009
Loading...

Share This Page