SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru alecrust's Avatar
    Join Date
    Jun 2007
    Location
    London, UK
    Posts
    728
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If no parent present

    Hi everyone,

    I'm having a little problem with my JavaScript which I'm sure is easily resolvable.

    I have a link that opens a popup. This is fine. This popup has some links in it, which when clicked will open the link in the original parent window, then close the popup.

    Here's the JavaScript:

    Code:
    <script type="text/javascript">
    <!--
      function refreshOpener(newURL)
      {
        parent.opener.location=newURL;
        parent.close();
      }
    //-->
    </script>
    And the anchor:

    Code:
    <a href="javascript:refreshOpener('http://www.google.com/');">Go to Google</a>
    This all works fine. However the problem is sometimes this method is used not in a popup but just a normal page. And there's nothing I can do about it.

    So in this case, since there is no parent window, the link doesn't work.

    Can anybody tell me what I should modify in the JavaScript to basically say "if there is a parent, then open it in that. If not, open it in a new window".

    I hope I've made sense. Thanks again!
    Honda Civic Forum - UK Civic Forum
    Web Host Reviews - Independent, honest host reviews
    My Portfolio - Simplicity is the ultimate sophistication

  2. #2
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    You can try

    Code:
      if(window.opener) {
        parent.opener.location=newURL;
        parent.close();
      } else {
        window.open(...);
      }
    Hope this helps

  3. #3
    SitePoint Guru alecrust's Avatar
    Join Date
    Jun 2007
    Location
    London, UK
    Posts
    728
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that. Don't understand the (...) bit though?

    And surely it would then be this instead:

    Code:
    <script type="text/javascript">
    <!--
      function refreshOpener(newURL)
      if(window.opener) {
        parent.opener.location=newURL;
        parent.close();
      } else {
        window.opener.location=newURL;
      }
    //-->
    </script>
    Is this correct?

    Thanks again.
    Honda Civic Forum - UK Civic Forum
    Web Host Reviews - Independent, honest host reviews
    My Portfolio - Simplicity is the ultimate sophistication

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The parent reference is unnecessary and will just map to self/window.

    Code:
    <script type="text/javascript">
    function refreshOpener(newURL)
    {
      if(window.opener) 
      {
        window.opener.location.href=newURL;
        window.close();
      } 
      else 
        window.location.href=newURL;
    } 
    </script>
    Tab-indentation is a crime against humanity.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,820
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    That will open the new page in the current window if a parent window doesn't exist. If you want it to open in a new window then you need to use window.open instead of window.location
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by alecrust
    Thanks for that. Don't understand the (...) bit though?
    The (...) is the bit you fill in. In other words the arguments for the window.open() function. (... is ellipsis in English). I don't know what the specific arguments you need to fill in are, so I just added the ...

    The section of code you posted is slightly wrong. Ill try explain why.
    Code:
      // if statement checking if window.opener exists
      if(window.opener) {
        parent.opener.location=newURL;
        parent.close();
      } else {
        // in the else part of the if statement (because window.opener doesn't 
        // exist) so you cant use it here. You can only use it in the if part above.
        window.opener.location=newURL;
      }

  7. #7
    SitePoint Guru alecrust's Avatar
    Join Date
    Jun 2007
    Location
    London, UK
    Posts
    728
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your help everyone. This is the solution I went with in the end. Let me know if you think it's wrong!

    Code:
    <script type="text/javascript">
    <!--
    	function refreshOpener(newURL) {
      if(parent.opener) {
        parent.opener.location=newURL;
        parent.close();
      } else {
        window.location.href=newURL;
      }
    	}
    //-->
    </script>
    Honda Civic Forum - UK Civic Forum
    Web Host Reviews - Independent, honest host reviews
    My Portfolio - Simplicity is the ultimate sophistication

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,820
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Is this code in a frame within the popup? If not then why are you trying to reference parent.opener instead of window.opener?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •