开发者

Undefined Javascript variable

So here is the problem. There is a HTML/JS code, but I can't read v3 variable. In short anything after DDDD(D,{"COM":"lng","leaf":145,"AXIS":true}); (which is some kind of predefined random array) is unreadable(or ignored as JS code). Why? And how can i get contents of v3? I开发者_开发百科s this a javascript parse bug?

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script type="text/javascript">
  <!--
  var v1 = 12345;
  var v2 = "Hello world";
  DDDD(D,{"COM":"lng","leaf":145,"AXIS":true});
        var v3 = "World Hello!!!"; 
  //-->
</script>
</head>

<!-- some html code -->
<script>

alert("This is "+v3);

</script>

<!-- some html code -->
</html>


Your first script crashes because you don't have a DDDD function, so the v3 never gets assigned.

You refer to the DDDD line as "which is some kind of predefined random array". It's not.

It is an attempt to call a function, and pass it two arguments.

  • DDDD() a function call.

  • a D variable argument.

  • a {"COM":"lng","leaf":145,"AXIS":true} object literal argument.


I assume that D and DDDD are defined somewhere? Your code excerpt doesn't define them. If they're defined, I'm not seeing the error; if they aren't, well, that's your problem.


Your problem is that the DDDD() line throws an exception because it uses an undefined function (DDDD is not defined), then anything that follows inside that script tag is not executed. The second script tag is executed however, but it doesn't have access to the variable that was never defined.


You can catch errors and then v3 will come out fine:

<script type="text/javascript">
      <!--
      var v1 = 12345;
      var v2 = "Hello world";
      try {
         DDDD(D,{"COM":"lng","leaf":145,"AXIS":true});
      }
      catch (ex) {
         alert("error: " + ex.message);
      }
      var v3 = "World Hello!!!"; 
      alert(v3);
      //-->

</script>


There is an error at line

DDDD(D,{"COM":"lng","leaf":145,"AXIS":true});

When an error occurs. Remaining JS is not executed.


When javascript gets an error in a line (depends the egine) it either breaks or fails to load entirely. And like most people already said before me, the DDDD() has to exist somewhere else is undefined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜