Getting the url of a new window.

Discussion in 'JavaScript' started by mahmood, Nov 6, 2007.

  1. #1
    Can we get the url of a new window which has been opened by window.open function?
    Lets say we open a new window and and change the url of this new window. Can we get the new url now?
     
    mahmood, Nov 6, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Depends if the new URL is still within the same domain name. If it is, then you can. Otherwise, you can't (the browsers do not allow you to do this, for security reasons).

    
    var mywin = window.open('page2.html');
    function checkUrl ()
    {
       alert("Url is: " + mywin.location.href)
    }
    
    Code (markup):
     
    phper, Nov 6, 2007 IP
  3. prasadkrn

    prasadkrn Member

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    hii

    the window.open function you called will always return a reference to the new child window that has opened. So through that object u can access many of the child window properties. Thus u can get the URL of the child window also.

    thanks
    Prasad
     
    prasadkrn, Nov 8, 2007 IP
    mahmood likes this.
  4. mahmood

    mahmood Guest

    Messages:
    1,228
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have this script on a page:
    <script type='text/javascript'>
    	var myWindow = window.open("http://www.google.com");
    	alert(myWindow.location.href);
    </script>
    PHP:
    IE doesn't give me any alert message at all and FF pops an alert with this text "about:blank"

    What is the problem then?
     
    mahmood, Nov 8, 2007 IP
  5. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    As I said earlier, if the URL in the child window has a different domain name, the browser doesn't allow you to access it anymore.
     
    phper, Nov 8, 2007 IP