Check if a window is open

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack

    Check if a window is open

    I have a main webpage that has a list of records, each with a link to a
    window.open function call. As an example, a page that opens is
    editrecord.aspx ?RecordID=34, and another is
    editrecord.aspx ?RecordID=52. If the user starts changing the contents
    of the opened window, and then leaves it open and goes back to the main
    page, refreshes the list, and clicks on the same record link again, it
    reloads the contents of the page the user had changed, and all the
    changes they made are lost. Is there a way to check to see if the
    window for that record is open before we reload it, so that I can just
    call the focus() function on that window instead of window.open again?

  • Manifest Interactive

    #2
    Re: Check if a window is open


    Jack wrote:[color=blue]
    > I have a main webpage that has a list of records, each with a link to a
    > window.open function call. As an example, a page that opens is
    > editrecord.aspx ?RecordID=34, and another is
    > editrecord.aspx ?RecordID=52. If the user starts changing the contents
    > of the opened window, and then leaves it open and goes back to the main
    > page, refreshes the list, and clicks on the same record link again, it
    > reloads the contents of the page the user had changed, and all the
    > changes they made are lost. Is there a way to check to see if the
    > window for that record is open before we reload it, so that I can just
    > call the focus() function on that window instead of window.open again?[/color]

    Greetings,

    Here is a solution that will allow you to check to see if a child
    window is open from the parent that launched it. This will bring a
    focus to the child window without reloading its data:

    <script language="JavaS cript">
    function loadUniquePage( page) {
    if (opener && !opener.closed) {
    opener.focus();
    }
    else {
    var myWin = window.open(pag e,'','width=800 ,height=600');
    opener = myWin;
    }
    }
    </script>

    <a href="javascrip t:loadUniquePag e('http://www.google.com' )">link</a>

    Notice that the link has to be formated like it is above when you
    include it in your page for this to work. Just pass it the URL you want
    to have opened and voila!

    ** NOTE: If the user refreshes the parent window, it loses all its
    references to any child windows it may have had open.

    I have placed this code on my server if you want to just copy the code
    from there:

    Senior full-stack engineer, technical lead, and AI developer building scalable products, ecommerce systems, and practical AI workflows.


    Hope this helps,
    - Peter Schmalfeldt

    Comment

    • RobG

      #3
      Re: Check if a window is open

      Manifest Interactive said on 17/03/2006 8:26 AM AEST:[color=blue]
      > Jack wrote:
      >[color=green]
      >>I have a main webpage that has a list of records, each with a link to a
      >>window.open function call. As an example, a page that opens is
      >>editrecord.as px?RecordID=34, and another is
      >>editrecord.as px?RecordID=52. If the user starts changing the contents
      >>of the opened window, and then leaves it open and goes back to the main
      >>page, refreshes the list, and clicks on the same record link again, it
      >>reloads the contents of the page the user had changed, and all the
      >>changes they made are lost. Is there a way to check to see if the
      >>window for that record is open before we reload it, so that I can just
      >>call the focus() function on that window instead of window.open again?[/color]
      >
      >
      > Greetings,
      >
      > Here is a solution that will allow you to check to see if a child
      > window is open from the parent that launched it. This will bring a
      > focus to the child window without reloading its data:
      >
      > <script language="JavaS cript">[/color]

      The language attribute is deprecated, type is required:

      <script type="text/javascript">

      [color=blue]
      > function loadUniquePage( page) {
      > if (opener && !opener.closed) {[/color]

      Global variables should be declared with the 'var' keyword, just like
      other variables, but outside the scope of other objects.

      It's also a bad idea to use 'opener', which is already a DOM 0 property
      of window objects - it refers to the window that opened this current
      window, if there is one.

      var popWin;
      function loadUniquePage( page)
      {
      if (popWin&& !popWin.closed && popWin.focus){

      Methods of host objects should be tested before use.

      [color=blue]
      > opener.focus();[/color]

      This will not guarantee that 'opener' is brought to the front, but worth
      a try. If the window is at the back, nothing will seem to be happening
      for some users - no new page, no 'focused popup, nothing. They will
      have to close the pop-up to get it to work again.

      That may not be desirable, but it's a 'feature' of using pop-ups this way.

      [color=blue]
      > }
      > else {
      > var myWin = window.open(pag e,'','width=800 ,height=600');
      > opener = myWin;[/color]

      Why create a local variable only to re-assign the reference to the
      already created global variable?

      popWin = window.open(pag e,'','width=800 ,height=600');

      [color=blue]
      > }
      > }
      > </script>[/color]

      The full script:

      <script type="text/javascript">
      var popWin;
      function popPage(url)
      {
      if (popWin &! popWin.closed && popWin.focus){
      popWin.focus();
      } else {
      popWin = window.open(url ,'','width=800, height=600');
      }
      }
      </script>

      [color=blue]
      >
      > <a href="javascrip t:loadUniquePag e('http://www.google.com' )">link</a>[/color]

      And users without script support will see a link that does nothing.
      Better to use:

      <a href="http://www.google.com"
      onclick="popPag e(this.href);re turn false;">link</a>


      At least then non-scripted UAs can navigate to the link.

      [...]


      --
      Rob

      Comment

      • RobG

        #4
        Re: Check if a window is open

        RobG said on 17/03/2006 10:05 AM AEST:
        [...][color=blue]
        > The full script:
        >
        > <script type="text/javascript">
        > var popWin;
        > function popPage(url)
        > {
        > if (popWin &! popWin.closed && popWin.focus){[/color]

        Aggh, gremlins in the works...

        if (popWin && !popWin.closed && popWin.focus){

        [color=blue]
        > popWin.focus();
        > } else {
        > popWin = window.open(url ,'','width=800, height=600');
        > }
        > }
        > </script>[/color]

        [...]

        --
        Rob

        Comment

        • Randy Webb

          #5
          Re: Check if a window is open

          RobG said the following on 3/16/2006 7:05 PM:[color=blue]
          > Manifest Interactive said on 17/03/2006 8:26 AM AEST:[/color]

          <snip>
          [color=blue][color=green]
          >>
          >> <a href="javascrip t:loadUniquePag e('http://www.google.com' )">link</a>[/color]
          >
          > And users without script support will see a link that does nothing.
          > Better to use:
          >
          > <a href="http://www.google.com"
          > onclick="popPag e(this.href);re turn false;">link</a>
          >
          >
          > At least then non-scripted UAs can navigate to the link.
          >[/color]

          And if there is an error in popPage then the JS enabled user gets nothing :)

          <a href="..." onclick="return popPage(this.hr ef)">

          And have popPage return false at the end.

          --
          Randy
          comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
          Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

          Comment

          • Greg

            #6
            Re: Check if a window is open

            What if main page goes through a postback and the variable is lost? is
            there a way to still check if a child window created using the
            window.open method still exsits.

            Is there a way if I know the name of the window or is there a way to
            store the child window handle so that after a refresh on the main page
            I can still check if that same page is still open?

            Then if that is true can i still setfocus on that child window?

            Comment

            • Randy Webb

              #7
              Re: Check if a window is open

              Greg said the following on 3/17/2006 9:26 AM:[color=blue]
              > What if main page goes through a postback and the variable is lost? is
              > there a way to still check if a child window created using the
              > window.open method still exsits.[/color]

              No. Once the reference is lost it is lost. You have to recreate it and
              you run into the focus problem where applying focus() to the window may
              focus it but doesn't bring it back to the front.
              [color=blue]
              > Is there a way if I know the name of the window or is there a way to
              > store the child window handle so that after a refresh on the main page
              > I can still check if that same page is still open?[/color]

              No.
              [color=blue]
              > Then if that is true can i still setfocus on that child window?[/color]

              See above.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
              Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

              Comment

              Working...