<HTML>
  <HEAD>
    <TITLE>Script 2.3 - A demonstration of variable
scope</TITLE>
    <SCRIPT LANGUAGE = "JavaScript" TYPE = "Text/JavaScript">
    <!-- Start hiding JavaScript statements
      glVarMsg1 = "Global variables created in the HEAD section can be " + "referenced anywhere in this page.";
      function CreateVariables()
      {
      var lcVarMsg1 = "Local variables created inside a function cannot " + "be referenced anywhere else.";
      glVarMsg2 = "Global variables created inside functions in the HEAD " + "section can be referenced anywhere in this page.";
      }
    // End hiding JavaScript statements -->
    </SCRIPT>
  </HEAD>
  <BODY>
<SCRIPT LANGUAGE = "JavaScript" Type = "Text/JavaScript">
    <!-- Start hiding JavaScript statements
      CreateVariables();
      glVarMsg3 = "Global variables created in the BODY section can be " + "referenced anywhere in this page."; 
      document.write(glVarMsg1 + "<BR>");
      document.write(glVarMsg2 + "<BR>");
      document.write(glVarMsg3 + "<BR>");
      document.write(lcVarMsg1 + "<BR>");
    // End hiding JavaScript statements -->
    </SCRIPT>
  </BODY>
</HTML>你在字符串中间加上了回车
所以就会出错了。。

解决方案 »

  1.   

    只是好多,你不能分成几行写比如:glVarMsg1="XXX"+"XXX"你不可以再分行
    正确地如下:
    <HTML>
      <HEAD>
        <TITLE>Script 2.3 - A demonstration of variable
    scope</TITLE>
        <SCRIPT LANGUAGE = "JavaScript" TYPE = "Text/JavaScript">
        <!-- Start hiding JavaScript statements
          glVarMsg1 = "Global variables created in the HEAD section can be " + "referenced anywhere in this page.";
          function CreateVariables()
          {
          lcVarMsg1 = "Local variables created inside a function cannot " + "be referenced anywhere else.";
          glVarMsg2 = "Global variables created inside functions in the HEAD " +        "section can be referenced anywhere in this page.";
          }
        // End hiding JavaScript statements -->
        </SCRIPT>
      </HEAD>
      <BODY>
    <SCRIPT LANGUAGE = "JavaScript" Type = "Text/JavaScript">
        <!-- Start hiding JavaScript statements
          CreateVariables();
          glVarMsg3 = "Global variables created in the BODY section can be " +  "referenced anywhere in this page."; 
          document.write(glVarMsg1 + "<BR>");
          document.write(glVarMsg2 + "<BR>");
          document.write(glVarMsg3 + "<BR>");
          document.write(lcVarMsg1 + "<BR>");
        // End hiding JavaScript statements -->
        </SCRIPT>
      </BODY>
    </HTML>
      

  2.   

    代码问题:
    1、var 变量名 = "XXXXXXXXX" + "YYYYYYY"时"XXXXXXXXX"之间不能有回车,但+也是表示联接符可以有回车换行;
    因此:
    var 变量名 = "XXXXXXXXX" 
                 + "YYYYYYY"
    或者
    var 变量名 = "XXXXXXXXX" 
                 + 
                 "YYYYYYY"
    这样的写法都是正确的;
    但:
    var 变量名 = "XXXXXXX
                  XX" + "YYYYYYY"
    是错误的;并且你的CreateVariables()函数没有返回值,但又要在函数外面调用里面的变量,肯定会报错;若想让函数内的两个变量内容能显示出来可以这样写:
    <HTML>
      <HEAD>
        <TITLE>Script 2.3 - A demonstration of variable
    scope</TITLE>
        <SCRIPT LANGUAGE = "JavaScript" TYPE = "Text/JavaScript">
        <!-- Start hiding JavaScript statements
          glVarMsg1 = "Global variables created in the HEAD section can be " 
      +
            "referenced anywhere in this page.";
          function CreateVariables()
          {
        var lcVarMsg1 = new Array();
      lcVarMsg1[0] = "Local variables created inside a function cannot " 
      + "be referenced anywhere else.";
           lcVarMsg1[1] = "Global variables created inside functions in the HEAD " 
      + "section can be referenced anywhere in this page.";
    return lcVarMsg1;
          }
        // End hiding JavaScript statements -->
        </SCRIPT>
      </HEAD>
      <BODY>
    <SCRIPT LANGUAGE = "JavaScript" Type = "Text/JavaScript">
        <!-- Start hiding JavaScript statements
          var lcVarMsg1 = CreateVariables();
          glVarMsg3 = "Global variables created in the BODY section can be " +
            "referenced anywhere in this page."; 
          document.write(glVarMsg1 + "<BR>");
          document.write(lcVarMsg1[0] + "<BR>");
          document.write(glVarMsg3 + "<BR>");
          document.write(lcVarMsg1[1] + "<BR>");
        // End hiding JavaScript statements -->
        </SCRIPT>
      </BODY>
    </HTML>