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

Posted on 2004-11-14
Last Modified: 2011-09-20

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

            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";

Here's the validatePassword.js

function validatePassword(id1, id2)
      ctl1 = document.getElementById(id1);
      ctl2 = document.getElementById(id2);
      if (ctl1.value == ctl2.value)
            window.returnValue = ctl1.value;
            alert('Passwords do not match');

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,

Question by:datamobility
    LVL 25

    Expert Comment

    guess it shud be this way
      ctl1 = document.getElementById('id1');
         ctl2 = document.getElementById('id2');
    LVL 25

    Accepted Solution

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

    should have closed tag:
    <script type="text/javascript" src="./validatePassword.js"/></script>
    LVL 21

    Expert Comment

    if you are using .net 2003 you can find out which part of the javascript went wrong

    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.


    Author Comment

    > [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?

    LVL 25

    Assisted Solution

    datamobility, I'm not sure for aspx syntax, need learn it too :)
    but anyway, if your aspx is ready on server, it sends to client plain html and in this html must be the closing tag </script>

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now