Fontfaced
Fontfaced

Reputation: 29

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

Upvotes: 2

Views: 317

Answers (6)

Shadow Wizard
Shadow Wizard

Reputation: 66389

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>

Upvotes: 1

user113716
user113716

Reputation: 322452

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.

Upvotes: 5

elasticrash
elasticrash

Reputation: 1221

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.

Upvotes: 0

Seldaek
Seldaek

Reputation: 42026

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.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1073968

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.

Upvotes: 1

Tasawer Khan
Tasawer Khan

Reputation: 6148

There is an error at line

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

When an error occurs. Remaining JS is not executed.

Upvotes: 0

Related Questions