/    Sign up×
Community /Pin to ProfileBookmark

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([url]www.bookstore.com[/url]) 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.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManJun 05.2009 — 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 [I]should[/I] be able to redirect it.
Copy linkTweet thisAlerts:
@tekkyauthorJun 05.2009 — 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.
Copy linkTweet thisAlerts:
@Angry_Black_ManJun 05.2009 — here's an example:

var openerObj = window.opener
window.close()
openerObj.document.location.href = "somelocation.com"
Copy linkTweet thisAlerts:
@tekkyauthorJun 06.2009 — Thank you so much Aaron for your help.
Copy linkTweet thisAlerts:
@tekkyauthorJun 08.2009 — 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

}




here's an example:

var openerObj = window.opener
window.close()
openerObj.document.location.href = "somelocation.com"
[/QUOTE]
Copy linkTweet thisAlerts:
@Angry_Black_ManJun 09.2009 — right after window.close(), place the line "alert(openerObj)".. what do you get?
Copy linkTweet thisAlerts:
@tekkyauthorJun 09.2009 — 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.
Copy linkTweet thisAlerts:
@tekkyauthorJun 09.2009 — 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.
×

Success!

Help @tekky spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...