Loading ...
Sorry, an error occurred while loading the content.
 

Getting JS error message. Why?

Expand Messages
  • Frank
    Hi everyone, I am trying to do a very simple CSS file switch to change the skin of my site by toggling though with a button in ASP.NET and function calls in
    Message 1 of 5 , Sep 3 6:03 PM
      Hi everyone,

      I am trying to do a very simple CSS file switch to change the skin of
      my site by toggling though with a button in ASP.NET and function
      calls in JavaScript. Here is the pertanent ASP.NET code:

      <input type="button" id="btnSkin" value="Change Skin" onclick="Skin2
      ();" />

      Here are the JS functions:

      <script type="text/javascript">
      function Skin1()
      {
      link[0].setAttribute("disabled", "false");
      link[1].setAttribute("disabled", "true");
      btn.onclick = Skin2;
      }

      function Skin2()
      {
      link[0].setAttribute("disabled", "true");
      link[1].setAttribute("disabled", "false");
      btn.onclick = Skin1;
      }
      </script>

      And here is the error message when pressing the "btnSkin" button:

      "Microsoft JScript runtime error: 'btn' is null or not an object"

      Any odeas as to what is going on? I can't seem to get any SIMPLE JS
      to run within my projects. Wierd. Thanks for any help!

      Frank

      P.S. Here are the two CSS file calls, btw:

      <link rel="stylesheet" type="text/css" href="koreacss.css" />
      <link rel="alternate stylesheet" type="text/css"
      href="koreacss2.css" />
    • Frank
      I forgot to mention that I did declare the button, by the way. Here is the code (outside of the functions): var link = document.getElementsByTagName( link );
      Message 2 of 5 , Sep 4 12:46 PM
        I forgot to mention that I did declare the button, by the way. Here
        is the code (outside of the functions):

        var link = document.getElementsByTagName("link");
        var btn = document.getElementById("btnSkin");

        Still getting error, however.
        Thanks again,

        Frank

        --- In JavaScript_Official@yahoogroups.com, "Frank" <maybush1@...>
        wrote:
        >
        > Hi everyone,
        >
        > I am trying to do a very simple CSS file switch to change the skin
        of
        > my site by toggling though with a button in ASP.NET and function
        > calls in JavaScript. Here is the pertanent ASP.NET code:
        >
        > <input type="button" id="btnSkin" value="Change Skin" onclick="Skin2
        > ();" />
        >
        > Here are the JS functions:
        >
        > <script type="text/javascript">
        > function Skin1()
        > {
        > link[0].setAttribute("disabled", "false");
        > link[1].setAttribute("disabled", "true");
        > btn.onclick = Skin2;
        > }
        >
        > function Skin2()
        > {
        > link[0].setAttribute("disabled", "true");
        > link[1].setAttribute("disabled", "false");
        > btn.onclick = Skin1;
        > }
        > </script>
        >
        > And here is the error message when pressing the "btnSkin" button:
        >
        > "Microsoft JScript runtime error: 'btn' is null or not an object"
        >
        > Any odeas as to what is going on? I can't seem to get any SIMPLE JS
        > to run within my projects. Wierd. Thanks for any help!
        >
        > Frank
        >
        > P.S. Here are the two CSS file calls, btw:
        >
        > <link rel="stylesheet" type="text/css" href="koreacss.css" />
        > <link rel="alternate stylesheet" type="text/css"
        > href="koreacss2.css" />
        >
      • kislife2004
        Hi Frank, I m not sure whether you have declare the variable btn . According to the error message, btn seems to be not declared :-). Well, as for me ,I prefer
        Message 3 of 5 , Sep 4 5:22 PM
          Hi Frank,

          I'm not sure whether you have declare the variable "btn". According
          to the error message, btn seems to be not declared :-).

          Well, as for me ,I prefer to use a flag to implement this css shift
          function. I mean using a variable "flag" which is true or false to
          identify whether the current CSS style is A or B . It could be this :

          flag = true;

          function chageSkin()
          {
          if(flag){
          link[0].setAttribut e("disabled" , "true");
          link[1].setAttribut e("disabled" , "false");
          flag=false;
          }else{
          link[0].setAttribut e("disabled" , "false");
          link[1].setAttribut e("disabled" , "true");
          flag=true;
          }
          }
        • Paul Donahue
          Hi Frank, The script is trying to access btnSkin before the body has loaded. If you declare the variable btn inside your functions the error will probably
          Message 4 of 5 , Sep 4 6:35 PM
            Hi Frank,

            The script is trying to access 'btnSkin' before the body has loaded.
            If you declare the variable 'btn' inside your functions the error
            will probably go away.

            Paul


            --- In JavaScript_Official@yahoogroups.com, "Frank" <maybush1@...>
            wrote:
            >
            > I forgot to mention that I did declare the button, by the way. Here
            > is the code (outside of the functions):
            >
            > var link = document.getElementsByTagName("link");
            > var btn = document.getElementById("btnSkin");
            >
            > Still getting error, however.
            > Thanks again,
            >
            > Frank
            >
            > --- In JavaScript_Official@yahoogroups.com, "Frank" <maybush1@>
            > wrote:
            > >
            > > Hi everyone,
            > >
            > > I am trying to do a very simple CSS file switch to change the
            skin
            > of
            > > my site by toggling though with a button in ASP.NET and function
            > > calls in JavaScript. Here is the pertanent ASP.NET code:
            > >
            > > <input type="button" id="btnSkin" value="Change Skin"
            onclick="Skin2
            > > ();" />
            > >
            > > Here are the JS functions:
            > >
            > > <script type="text/javascript">
            > > function Skin1()
            > > {
            > > link[0].setAttribute("disabled", "false");
            > > link[1].setAttribute("disabled", "true");
            > > btn.onclick = Skin2;
            > > }
            > >
            > > function Skin2()
            > > {
            > > link[0].setAttribute("disabled", "true");
            > > link[1].setAttribute("disabled", "false");
            > > btn.onclick = Skin1;
            > > }
            > > </script>
            > >
            > > And here is the error message when pressing the "btnSkin" button:
            > >
            > > "Microsoft JScript runtime error: 'btn' is null or not an object"
            > >
            > > Any odeas as to what is going on? I can't seem to get any SIMPLE
            JS
            > > to run within my projects. Wierd. Thanks for any help!
            > >
            > > Frank
            > >
            > > P.S. Here are the two CSS file calls, btw:
            > >
            > > <link rel="stylesheet" type="text/css" href="koreacss.css" />
            > > <link rel="alternate stylesheet" type="text/css"
            > > href="koreacss2.css" />
            > >
            >
          • Jon Stephens
            ... JavaScript functions don t have automatic access to user-defined global variables. ... Either define link and btn inside the functions, or prefix them with
            Message 5 of 5 , Sep 5 7:53 AM
              > Re: Getting JS error message. Why?
              > Posted by: "Frank" maybush1@... maybush1
              > Date: Tue Sep 4, 2007 2:29 pm ((PDT))
              >
              > I forgot to mention that I did declare the button, by the way. Here
              > is the code

              This is [*part* of] the source of the problem:

              > (outside of the functions):

              JavaScript functions don't have automatic access to user-defined global
              variables.

              >
              > var link = document.getElementsByTagName("link");
              > var btn = document.getElementById("btnSkin");
              >
              > Still getting error, however.
              > Thanks again,

              Either define link and btn inside the functions, or prefix them with a
              reference to the global Window object, e.g.

              window.link[0].setAttribute("disabled", "false");

              But a better way to do this would be to define a single function and a
              single global variable to hold the state:

              function toggle()
              {
              var link = document.getElementsByTagName("link");

              link[0].disabled = window.theState;
              window.theState = !window.theState;
              link[1].disabled = window.theState;
              }

              var theState = true;

              ...

              <input type="button" value="Change Skin" onclick="toggle();"/>

              >
              > Frank

              cheers

              jon.


              --
              This message has not been scanned for viruses.

              Since I do not use a Microsoft operating
              system or software, and use only plaintext
              for email, there is little need for me to do so.
            Your message has been successfully submitted and would be delivered to recipients shortly.