问题描述:

I have a html page and I'm calling a javascript function from the html page and the script is in separate file

HTML

<a href="javascript:void(0);" id="btn1" onclick="ButtonClick();">Click Me</a>

<script src="Externalscript.js"></script>

<script>

Initialize('1,2,3,4');

</script>

JavaScript

var myList; //global variable

function Initialize(ids) {

myList = new Array();

var idList = ids.split(',');

for (i = 0; i < idList.length; i++) {

myList.push(idList[i]);

}

I'm storing the ids value in myList and now I want to retrieve the myList when the button clicked

 function ButtonClick() {

alert(myList);

}

Now, when I'm trying to alert the myList it is giving undefined.

How can store the values in a global variable and retrieve in other function

网友答案:

return the value from function than updating global-variable and accessing it

function ButtonClick() {
  var returned = Initialize('1,2,3,4');
  console.log(returned);
}

function Initialize(ids) {
  var myList = new Array();
  var idList = ids.split(',');
  for (i = 0; i < idList.length; i++) {
    myList.push(idList[i]);
  }
  return myList;
}
<a href="javascript:void(0);" id="btn1" onclick="ButtonClick();">Click Me</a>
网友答案:
<!DOCTYPE html >
<html>
<head>
<script>
    Initialize('1,2,3,4');
    var myList;  //global variable
          function Initialize(ids)
          {
          myList = new Array();
          var idList = ids.split(',');
          for (i = 0; i < idList.length; i++)
                   {
                    myList.push(idList[i]);
                   }
          }

         function ButtonClick() 
          {
               alert(myList);
         }
</script>
        </head>
        <body>
        <a href="javascript:void(0);" id="btn1" onclick="ButtonClick();">Click Me</a>
        </body>
        </html>

I just rewrote your code and it prints the list as 1,2,3,4 as expected

网友答案:

I'm having trouble reconstructing the problem. Is this how your code looks like:

<a href="javascript:void(0);" id="btn1" onclick="alert(myList)">Click Me</a>

<script>

var myList;  //global variable
function Initialize(ids) {
    myList = new Array();
    var idList = ids.split(',');
    for (i = 0; i < idList.length; i++) {
      myList.push(idList[i]);
    }
 }

 Initialize('1,2,3,4');
</script>

http://codepen.io/calvinclaus/pen/WxNaPX?editors=1011

Because this works...Can you supply a pen with the problem ?

网友答案:

Your code should work.

One advice: split already creates an array: MDN

So what you're doing is:

  • create an array from a string (idList = ids.split(','))
  • loop over that array
  • add every value from that array to a other array (myList.push(idList[i]);)

Where myList = ids.split(','); is enough

Example

var myList; 

function Initialize(ids) {
    myList = ids.split(',');
}

Initialize('1,2,3,4');

function ButtonClick(e) {
    e.preventDefault();

    alert(myList);
}
document.getElementById('btn1').addEventListener('click', ButtonClick);

Working demo: fiddle

相关阅读:
Top