Link to home
Start Free TrialLog in
Avatar of datamobility
datamobility

asked on

javascript "object expected" error calling external script from modal dialog

Hello,

I have a password change dialog made of 2 Labels, 2 TextBoxes and 2 Buttons, which I display using showModalDialog.

Here's the Page_Load

<code>
            private void Page_Load(object sender, System.EventArgs e)
            {
                  sesh = HttpContext.Current.Session;
                  this.RegisterStartupScript("validatePassword", "<script type=\"text/javascript\" src=\"./validatePassword.js\"/>");

                  btnPwdOk.Attributes["onclick"] = string.Format("validatePassword('{0}', '{1}'); return false;", txtPwd1.ClientID, txtPwd2.ClientID);
                  btnPwdCancel.Attributes["onclick"] = "window.close(); return false";
            }
</code>


Here's the validatePassword.js

<code>
function validatePassword(id1, id2)
{
      ctl1 = document.getElementById(id1);
      ctl2 = document.getElementById(id2);
      if (ctl1.value == ctl2.value)
      {
            window.returnValue = ctl1.value;
      }
      else
      {
            alert('Passwords do not match');
      }
}
</code>

The problem is when I click on the OK button in the password dialog, I get this error:

Microsoft JScript runtime error: Object expected.

VS.NET then displays what I assume is the same as I'd get from "View Source" if right click actually worked on modals (anyway of enabling that btw?).  I can see that the script is registered and that the onclick attribute is set.  This is an extract:

<input type="submit" name="btnPwdOk" value="OK" id="btnPwdOk" onclick="validatePassword('txtPwd1', 'txtPwd2'); return false;"/>

<script type="text/javascript" src="./validatePassword.js"/>

I registered the script to display the modal in exactly the same and it works so I don't understand what I'm doing wrong.  


Thanks for any help,

.pd.
Avatar of archrajan
archrajan

guess it shud be this way
  ctl1 = document.getElementById('id1');
     ctl2 = document.getElementById('id2');
ASKER CERTIFIED SOLUTION
Avatar of devic
devic
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you are using .net 2003 you can find out which part of the javascript went wrong
Goto

IE ->Tools -> Internet Options -> Advanced -> Browsing

Uncheck "Disable Script Debugging"

So next time the error pops up you can debug it and find out which part went wrong.

regards,
Suraj
Avatar of datamobility

ASKER

> [devic] you chould have closed tag

Thank you for your response.  I made the change to my Page_Load and it now works but I am confused.  The popup form is displayed by another external script popupModel.js.  This script is registered using the same implicit tag-close:

<script type="text/javascript" src="modalPopup.js"/>  (note the slash before the closing angle bracket).

I was using exactly the same syntax for validatePassword but it doesn't work without the explicit closing tag.

Why does it work for one and not the other?  What is the difference in the way pages are loaded for modal dialogs?

.pd.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial