Solved

window.opener does not work in https, but ok in http

Posted on 2013-06-13
5
1,817 Views
Last Modified: 2013-06-13
Hello, I have a strange situation related to javascript window.opener.
I have 2 websites hosted in the same platform, UAT&PRD.
The only difference, beside the domain, is that UAT is on http, and PRD on https.

The below script works fine in UAT, but in PRD (https) I have the following error:

Error:
TypeError: window.opener.document.getElementById(...) is null

Open in new window


C#
System.Text.StringBuilder sbScript = new System.Text.StringBuilder();
                    sbScript.Append(@"setTimeout(function(){closePopup()},2000);window.opener.document.getElementById('" + anchorId + "').click();");
                    if (!ClientScript.IsStartupScriptRegistered("scriptTest"))
                        ClientScript.RegisterStartupScript(this.GetType(), "scriptTest", sbScript.ToString(), true);

Open in new window


javascript
function closePopup(){
    var opener = null;
    if (window.dialogArguments) // Internet Explorer supports window.dialogArguments
    { 
        opener = window.dialogArguments;
    } 
    else // Firefox, Safari, Google Chrome and Opera supports window.opener
    {        
        if (window.opener) 
        {
            opener = window.opener;
        }
    }       
    // write you code and refer "opener"
    window.close();
 };

Open in new window


To be mentioned that both PRD pages (opener and popup) are under https://
Thanks
0
Comment
Question by:danielivanov2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39246349
It is complaining the element is null, not that the opener does not exist.

Perhaps it is a security issue. Can you try lowering the security while testing?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 295 total points
ID: 39246632
Ahh

I am now reading the script hidden in the C code

sbScript.Append(@"setTimeout(function(){closePopup()},2000);window.opener.document.getElementById('" + anchorId + "').click();");
                   

will of course not work since the opener is gone

Try

sbScript.Append(@"setTimeout(function(){window.opener.document.getElementById('" + anchorId + "').click();closePopup()},2000);");

or put the click code in the closePopup where it says
// write you code and refer "opener"
as

if (opener) {
  var anchor = opener.document.getElementById('" + anchorId + "')
  if (anchor) anchor.click();
}
0
 

Author Comment

by:danielivanov2
ID: 39246658
Unfortunately, same result, ok in UAT, not ok in PRD, same javascript error, which is telling me that some sort of security is preventing this script to work.
I forgot to include in initial code the anchorId tag, in opener page:

<a id="anchorId" runat="server" onclick="return true" onserverclick="postback"></a>  

Open in new window


Below the script generated by Firebug, that is causing the error:
TypeError: window.opener.document.getElementById(...) is null

Open in new window


<script type="text/javascript">
//<![CDATA[
(function() {var fn = function() {$get("ToolkitScriptManager1_HiddenField").value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();setTimeout(function(){window.opener.document.getElementById(' MainContentPlaceHolder_anchorId').click();closePopup()},2000);(function() {var fn = function() {Sys.Extended.UI.ModalPopupBehavior.invokeViaServer('mpeTrainingFinished', true); Sys.Application.remove_load(fn);};Sys.Application.add_load(fn);})();Sys.Application.initialize(); 

Open in new window

0
 

Author Comment

by:danielivanov2
ID: 39246718
I have finally solved it, the anchorId should have been ctl00_MainContentPlaceHolder_anchorId and not MainContentPlaceHolder_anchorId, as initially set.
however, I will set the second answer as solution, as it is correcting my code
Thanks
0
 

Author Closing Comment

by:danielivanov2
ID: 39246720
The solution is posted in the last comment, however this post is showing the correct way to handle my script
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Find out all repos that a user is most active on Github 1 28
Hide and Unhide Table 6 24
Validating number not work with decimal 4 23
Slush on text 2 10
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question