views:

498

answers:

2

Hi All,

We currently have two asp.net 2.x web applications and we need to perform the following functionality:

From one application, we want to auto-login to the other web application automatically in a new tab; using the same browser instance/window.

So the process is:

Open New Window/Tab With Second System URL/Login Page Wait For Popup Window/Tab Page To Load (DOM Ready?)

OnPopupDomReady { Get Usename, Password, PIN Controls (jQuery Selectors) and Populate In Code Then Click Login Button (All Programatically). }

I am currently using JavaScript to Open the window as follows:

<script type="text/javascript">
  $(document).ready(function () {
    $('a[rel="external"]').click(function ()
       {
         window.open($(this).attr('href'));
         return false;
       });
      });  
</script>

I would like to use jQuery chaining functionality if possible to extent the method above so that I can attach a DOM Ready event to the popped up page and then use that event to call a method on the code behind of the popped up page to automatically login. Something similar to this (Note: The Following Code Sample Does Not Work, It Is Here To Try And Help Illustrate What We Are Trying To Achieve)...

 <script type="text/javascript">
    $(document).ready(function () {
      $('a[rel="external"]').click(function () {
        window.open($(this).attr('href').ready(function ()
        {
           // Use JavaScript (Pref. jQuery Partial Control Name Selectors) To Populate Username/Password TextBoxes & Click Login Button.
        })
      });

    });  
  </script>

Our Architecture Is As Follows: We have the source for both products (ASP.NET WebSite[s]) and they are run under different app. pools in IIS.

I hope this all makes sense, and if my plan is not going to work, please provide hints ;)

Thanks All/Kind Regards,

Terry Robinson.

A: 

When you open a window with window.open, the new window gets a property called window.opener which references the parent window. So code in your child window can call functions in the parent window, for instance:

In Window A:

// Declared at global scope => ends up as property on `window`
function phoneHome(str) {
    alert(str);
}

In Window B (the child window):

$.ready(function() {
    if (window.opener && window.opener.phoneHome) {
        window.opener.phoneHome("Hi, Ma!");
    }
});

(Using $.ready in the child window requires that the child window have jQuery loaded.)

In the above all I've done is have the child window trigger a function in the parent window with a message, but of course the function call can carry any data you want it to.

T.J. Crowder
A: 

Hi TJ, thanks for your help, I have tried this code and I get no alert at all from the parent side, code is as follows:

[CLIENT/CALLING CODE]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; 
<html xmlns="http://www.w3.org/1999/xhtml"&gt; 
<head><title> 

</title> 
  <script src="jquery-1.4.2.js" type="text/javascript"></script> 

  <script type="text/javascript" language="javascript" > 
    function phoneHome(str) {
      alert(str);
    };
  </script> 

  <script type="text/javascript" language="javascript"> 
    $(document).ready(function () {
      $('a[rel="external"]').click(function () {
        window.open($(this).attr('href'), 'LoginPage');
        return false;
      });
    });  
  </script> 
</head> 
<body> 
  <form name="form1" method="post" action="Host.aspx" id="form1"> 
<div> 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZB8MmxavqAJyjpTxMWlMQDN4W2SO" /> 
</div> 

  <div> 
    <a href="http://localhost:5741" rel="external" style="font-family: Arial, Helvetica, sans-serif; font-size: small">Load Login Page In New Tab</a> 
  </div> 
  </form> 
</body> 
</html>

[LOGIN PAGE]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt; 
<head><title> 

</title> 
  <script src="jquery-1.4.2.js" type="text/javascript" language="javascript"></script> 
  <script type="text/javascript" language="javascript"> 
    $(document).ready(function () {
      if (window.opener && window.opener.phoneHome) {
        window.opener.phoneHome("Hi, Ma!");
      }
    });
  </script> 
</head> 
<body> 
  <form name="form1" method="post" action="login.aspx" id="form1"> 
<div> 
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIODY2MjY4OThkZO5CgVjG+viijXUCkTET5P4wU1Oq" /> 
</div> 

<div> 

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBQKfpqTNAwKl1bK4CQK1qbSRCwKf+9KcDgKC3IeGDAVPLXdIKWWdDE6V2657BtYtdJfL" /> 
</div> 
  <div> 
    <input name="txtUsername" type="text" id="txtUsername" /><br /> 
    <input name="txtPassword" type="text" id="txtPassword" /><br /> 
    <input name="txtPIN" type="text" id="txtPIN" style="width:50px;" /><br /> 
    <input type="submit" name="btnLogin" value="Button" id="btnLogin" /> 
  </div> 
  </form> 
</body> 
</html>

Any ideas? TIA. Terry Robinson.

Terry Robinson
update, this code works fine unless it is run in a cross-domain environment. I have since switched methods and now pass in encrypted query strings which works fine for our purposes.
Terry Robinson