www.webdeveloper.com
Results 1 to 9 of 9

Thread: Need help with window.opener/window.opener.document

  1. #1
    Join Date
    Jun 2009
    Posts
    6

    Need help with window.opener/window.opener.document

    Hi,

    I have a page in which if i click a link will pops up a window(which has 2 dropdowns,checkbox and a button).
    And when the user selects an option in the dropdown, checks the checkbox and hits the button a new page needs to be populatde in the first page.
    For this i used
    <a href="javascript:void(0);" onclick="Image_Click()"><asp:button text="common" onclick="btnClick" runat="server"/></a>

    i wrote the function image_click using java script as below:

    <script type="text/javascript" language="javascript">

    function Image_Click()
    {
    var e=document.getElementbyId("option");
    var opt=e.options[e.selectedIndex].value;

    if (opt='book')
    {
    window.close();
    window.opener.document.location.href="www.bookstore.com";
    }
    else if(opt='pen')
    {
    window.close();
    window.opener.document.location.href="www.penstore.com";
    }

    else if(opt='pn')
    {
    window.close();
    window.opener.document.location.href="www.pnstore.com";
    }

    else if(opt='paper')
    {
    window.close();
    window.opener.document.location.href="www.paperstore.com";
    }

    else
    alert();


    }

    </script>

    When i open the link in the first or main page.the second page with dropdowns and checkboxes opens and when i select an option and than check the checkbox and proceed .only the first url(www.bookstore.com) page is opened regardless of what i select .This happens in both IE and Firefox.

    I tried both single and double quotes everywhere and also used window.opener.document.location and window.opener.location.href and nothing works.

    When i tried to run(debug) the aspx page it gives window.opener.document is null or not an object.

    Can Anyone please Help me in this.

    Thanks a Lot in Advance.

  2. #2
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    i think the issue is that once you do the window.close() method, the window object is destroyed. when you try to referene the opener property of the now NULL (destroyed) object, you get this issue.

    to resolve it, before you close() the window, make a variable called "opener" or something and assign it the "window.opener" value. after you close the window, the opener object will still exist and you should be able to redirect it.

  3. #3
    Join Date
    Jun 2009
    Posts
    6
    Hi Aaron,

    Thanks a Lot for your reply.
    But Iam not much familiar with java script window.opener and window.close().

    Can you please pls provide me with a sample code for this.

    I tried in below way:

    var opener;
    if (opt='book')
    {
    window.close();
    opener.href="www.bookstore.com";
    }
    else if(opt='pen')
    {
    window.close();
    opener.href="www.penstore.com";
    }

    I think this may be the wrong code as this didnot work by not doing anything.

    I appreciate your help
    Tekky.

  4. #4
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    here's an example:

    Code:
    var openerObj = window.opener
    window.close()
    openerObj.document.location.href = "somelocation.com"

  5. #5
    Join Date
    Jun 2009
    Posts
    6
    Thank you so much Aaron for your help.

  6. #6
    Join Date
    Jun 2009
    Posts
    6
    Hi Aaron,

    I tried below one in this way:

    function Image_Click()
    {

    var e=document.getElementById("GetName");//GetName is name of dropdown
    var opt=e.options[e.selectedIndex].value

    if(opt=="Sams")
    {
    var openerObj=window.opener
    window.close();
    openerObj.document.location.href="http://www.sams.com";
    }
    else if(opt==Costco")
    {
    var openerObj=window.opener
    window.close();
    openerObj.document.location.href="http://www.coscto.com";
    }
    elseif
    .....
    else
    {
    alert();

    Not sure why this doesnot work for me.

    When i ran this on the server,running line by line.It gave me an error saying do you want to close the window and then when i hit yes or no it says "Microsoft JScript runtime error: 'document' is null or not an object".

    And when i open the url on my local machine it does nothing when i select an option in the dropdown.

    Can you please give me some idea why i am returning with this error.

    Thanks a Lot
    Tekky
    }




    Quote Originally Posted by aaron.martinas View Post
    here's an example:

    Code:
    var openerObj = window.opener
    window.close()
    openerObj.document.location.href = "somelocation.com"

  7. #7
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    right after window.close(), place the line "alert(openerObj)".. what do you get?

  8. #8
    Join Date
    Jun 2009
    Posts
    6
    Thanks for replying me Aaron.
    Below are returned when i place an alert message after window.close();


    On the Server when i debug:

    Using IE:Firstly it says do you want to close the window,If yes/No than gives a warning message box saying "undefined".

    using Firefox:
    It gives a warning message box saying "null"


    On my local machine:

    using IE gives me a warning message box with [object] as text.

    Using Firefox just closes the second window and only the main window appears.

  9. #9
    Join Date
    Jun 2009
    Posts
    6
    Got this working.

    I used trim function before i compared my dropdown values becuase my dropdown value had many spaces.

    Thanks a Lot Aaron for you help.
    And Thank you everyone who checked my thread.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles