-3

ich brauche dringend Eure Hilfe, zur Verständnis. Im 1. Beispiel funktioniert mein Test Script normal. im 2. habe ich die beiden Abschnitte in jeweils eine Funktion gepackt und in der Console folgender Fehler angezeigt:

Uncaught ReferenceError: test is not defined : test_js.html:26

Frage, wie kann ich denn die Arrays in der 2. Funktion auslesen?

question

i've two functions. One with an array and one with a write function. I like to have the variable from function 1 to display in function 2. Hope you understand my problem?!

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<script language="javascript">
 
var test = new Array();

for(a=0; a<19;a++) {

test[a]= " bla bla "+a;
 }
 
</script>
<body>

 
<div>
<script language="javascript">
 
 
for(b=0; b<test.length;b++){
document.write('<a href="'+test[b]+'">'+test[b]+'</a><br><hr>');
} 

</script>
 </div>
</body>
</html>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<script language="javascript">
function a(){
var test = new Array();

for(a=0; a<19;a++) {

test[a]= " bla bla "+a;
 }
}

 a();
</script>
<body>

 
<div>
<script language="javascript">
function zeig(){
 
for(b=0; b<test.length;b++){
document.write('<a href="'+test[b]+'">'+test[b]+'</a><br><hr>');
}}
zeig();

</script>
 </div>
</body>
</html>
Micha
  • 1
  • 3
  • 9
    Much as I love learning German, you'll get more responses if you post in English. – gcampbell Aug 10 '16 at 14:35
  • Issue is `test` is defined in `function a` and is available only in `a`. You will have to make global or pass it as parameter. – Rajesh Aug 10 '16 at 14:38
  • Your problem is that test is scoped to the function 'a', it isn't a global variable. – Shilly Aug 10 '16 at 14:38
  • sry. I try... i've two functions. One with an array and one with a write function. I like to have the variable from function 1 to display in function 2. Hope you understand my problem?! – Micha Aug 10 '16 at 14:39
  • Ok Rajesh. But how to make it global ? thought if its declared as VAR=TEST it is global ?! – Micha Aug 10 '16 at 14:42
  • @Micha this should help you: [JSFiddle](https://jsfiddle.net/RajeshDixit/e0sq1ohe/). Also is it necessary to have 2 different script tags? – Rajesh Aug 10 '16 at 14:42
  • @ Rajesh and all other ! Your guys are awesome ! Thanks so mutch. That solved the problem ! – Micha Aug 10 '16 at 14:47
  • @Micha If you found an answer that solves your problem, mark it as accepted. If you found a solution that is was not provided as an answer, add it as an answer and mark that as accepted. – Ouroborus Aug 10 '16 at 14:50
  • @Micha I have added an answer. Have tried to be descriptive but if you have any queries, just drop a comment and someone will help help you. – Rajesh Aug 10 '16 at 14:59

1 Answers1

1

Few pointers:

  • Its a bad practice to pollute Global scope, so try to avoid it as much as possible. Why to avoid global variables
  • When you declare variables without var, they become global by default.
  • Its a bad practice to append DOM inside a loop. You should create a HTML string and perform a bulk operation.

Also, if it is possible to move JS out of HTML to a single file, you can avoid using global scope.

JSFiddle

(function() {
  // This is not a global variable. Its scope is limited to functions defined in IIFE
  var TEST = new Array();

  function a() {
    for (var a = 0; a < 19; a++) {
      TEST.push(" bla bla " + a);
    }
  }
  a();

  function zeig() {
    var _html = TEST.reduce(function(p, c) {
        return p + '<a href="' + c + '">' + c + '</a><br><hr>'
      }, "")
      /*for (var b = 0; b < TEST.length; b++) {
        _html += '<a href="' + TEST[b] + '">' + TEST[b] + '</a><br><hr>'
      }*/
    document.getElementById("content").innerHTML = _html;
  }

  window.addEventListener("load", zeig);
})()
<body>
  <div id="content">
  </div>
</body>

Note: I have used an array function(array.reduce) but have also kept for logic as comment. You can look-up to these functions later. Also you will notice I have renamed test to TEST. Its a convention that you should use all caps for global variables. Also if you use global variables, use should use window.VariableName. This will improve readability and help you in debug.

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79