JavaScript Error: Unable to get property name of undefined or null reference in ASP.Net

Last Reply 8 months ago By GolferGirl

Posted 9 months ago

I am encountering 2 js errors on page load with the code I posted,

1) Error: Unable to get property 'name' of undefined or null reference in this line of code behind: name = attrs[i].name;

and

2) SCRIPT5007: Unable to get property 'show' of undefined or null reference

web.config:

<sessionState mode="InProc" timeout="1" /> 
</system.web>

aspx.cs:

        //StartTimer() method is placed in Page_Load() event:

        protected void StartTimer()
        { 
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            if (!this.IsPostBack)            
            {
                Session["Reset"] = true;
                Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
                SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
                int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;   
                ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
            }
        }

aspx (between <script> tags):

        function SessionExpireAlert(timeout) {
            var totalSeconds = timeout / 1000;
            var minutes = Math.floor(totalSeconds / 60);
            var seconds = Math.floor(totalSeconds - minutes * 60);
            document.getElementById("seconds").innerHTML = parseInt(minutes) + " minutes " + parseInt(seconds) + " seconds";
            document.getElementById("secondsIdle").innerHTML = parseInt(minutes) + " minutes " + parseInt(seconds) + " seconds";
            setInterval(function() {
                totalSeconds--;
                minutes = Math.floor(totalSeconds / 60);
                seconds = Math.floor(totalSeconds - minutes * 60);
                document.getElementById("seconds").innerHTML = parseInt(minutes) + " minutes " + parseInt(seconds) + " seconds";
                document.getElementById("secondsIdle").innerHTML = parseInt(minutes) + " minutes " + parseInt(seconds) + " seconds";
            }, 1000);
            setTimeout(function() {
                //Show Popup before 60 seconds i.e. 1 minute 0 seconds of timeout.
                $find("mpeTimeout").show();
            }, timeout - 60 * 1000);
            setTimeout(function() {
                window.location = "Alerts/Expired.aspx";
            }, timeout);
        };
        function ResetSession() {
            //Redirect to refresh Session.
            window.location = window.location.href;
        } 

aspx

<asp:LinkButton ID="lnkFake" runat="server" /> 

<ajaxToolkit:ModalPopupExtender ID="mpeTimeout" runat="server" 
    BehaviorID="mpeTimeout" 
    PopupControlID="pnlPopup" 
    TargetControlID="lnkFake" 
    BackgroundCssClass="modalBackground" 
    OkControlID="btnYes" 
    OnOkScript="ResetSession()" 
    CancelControlID="btnNo">
</ajaxToolkit:ModalPopupExtender> 

 <asp:Panel ID="pnlPopup" runat="server" 
     CssClass="modalPopup" Style="text-align: center" BorderColor="#cee2e5" BorderStyle="Solid" BackColor="white" class="header1" >
    <div class="header1">Session Expiring!</div>
    <div class="body1">
        <asp:Label ID="Label1" runat="server" Text="Your Session will expire in "  Visible="true"></asp:Label>
        <span id="seconds"></span>
        <asp:Label ID="Label9" runat="server" Text="seconds. <br /> Do you want to continue?"> </asp:Label> 
    </div>
    <div class="footer1">
        <asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes" />
        <asp:Button ID="btnNo" runat="server" Text="No" CssClass="no" />
    </div>
</asp:Panel>

There are 2 errors happen on a solution where I integrated the javascript with a .net 3.5 solution using VS 2008, Win 7 and IE 11. (it works fine as a standalone-which I assume is how you are testing it). As soon as page loads, a webpage error dialog appears with error "unable to get property 'name' of undefined or null reference" when I click Yes it opens IE F12 debugger and the line of code the error refers to is what I posted.  The full code of the error is:

 

jQuery.fn.extend({
 data: function( key, value ) {
  var attrs, name,
   elem = this[0],
   i = 0,
   data = null;

  if ( key === undefined ) {
   if ( this.length ) {
    data = jQuery.data( elem );

    if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) {
     attrs = elem.attributes;
     for ( ; i < attrs.length; i++ ) {
      name = attrs[i].name;

      if ( !name.indexOf( "data-" ) ) {
       name = jQuery.camelCase( name.slice(5) );

       dataAttr( elem, name, data[ name ] );
      }
     }
     jQuery._data( elem, "parsedAttrs", true );
    }
   }

   return data;
  }

If I click No the program runs and the script works as expected.

Posted 8 months ago Modified on 8 months ago

I have concluded that this is working just fine after I deployed it to the server. The issue seems to be with the browser settings on my local machine, IE 11.  It throws the 2 errors but I if I click No to ignore the script behaves just fine.  When I deployed to the server, there are no browser errors so all seems good!