简体   繁体   中英

How do I assign the result of a function into a global variable?

I can't figure out how to assign this function's result into a global variable. I know this is a really basic thing, but can anyone help?

var pixel_code = null


function captureValue(){
  pixel_code = document.getElementById("baseText").value;
 return pixel_code;
}

pixel_code = captureValue();

You're reusing pixel_code in and out of the function, which is not a great pattern, but the code you show should work as expected. What error are you seeing? What code surrounds this code that you're not showing? Could all this perhaps be nested inside another function? (Thanks @JosephSilver for the nod.)

如果页面重新加载,您的变量将被重置为其初始状态。

Please try this,

var pixel_code='';

function captureValue(){
  return document.getElementById("baseText").value;
}

function getValueBack()
{
    pixel_code = captureValue();
    //alert(pixel_code); /* <----- uncomment to test -----<< */
}

Thanks for sharing the jsfiddle of what you were attempting. I see the concern. The captureValue() function is run asynchronously, so the console.log() shortly after defining it doesn't yet have a value. I've stripped and prodded the jsfiddle and come up with this working sample:

<html>
    <head>
    </head>
    <body>
    <h1>Welcome to the AdRoll SandBox</h1>

    <textarea id="baseText" style="width:400px;height:200px"></textarea><br />

    <input type="button" value="test" id="text_box_button" onclick="captureValue()"/>
    <input type="button" value="get" id="text_box_button2" onclick="getValue()"/>
    <script>
var pixel_code = null;

function captureValue(){
    pixel_code = document.getElementById("baseText").value;
    return false;
}

function getValue() {
    alert(pixel_code);
    return false;
}
    </script>
    </body>
</html>

I added a second button. Type in the textbox, push "test" (to set the value), then push "get" to get the value of the global variable.

Here's the same sample that uses jQuery and a closure to avoid the global variable:

<html>
    <head>
    </head>
    <body>
    <h1>Welcome to the AdRoll SandBox</h1>

    <textarea id="baseText" style="width:400px;height:200px"></textarea><br />

    <input type="button" value="test" id="text_box_button" />
    <input type="button" value="get" id="text_box_button2" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
$(document).ready(function () {
    var pixel_code = null;

    $("#text_box_button").click(function (){
        pixel_code = document.getElementById("baseText").value;
        return false;
    });

    $("#text_box_button2").click(function () {
        alert(pixel_code);
        return false;
    });
});
    </script>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM