Say I have a variable called “true” in a function called “test.” Then I have another function in a whole different script tag and I want to change “true” using my new function. How can I do this? Thanks.
<script type="text/javascript">
var hello="no";
if(hello=="yes"){
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "Message";
}
}
</script>
<script type="text/javascript">
function show(id) {
$('#' + id).show();
var hello="yes";
}
</script>
It doesnt seem to be working…
In your function, don’t use the
varkeyword. Doing so declares a different variablehelloin scope of the function.Update:
Your logic is faulty when calling the
onbeforeunload. You are never binding the event unlesshello == "yes", which it never does when that runs. Instead, check the variable contents in theconfirmExit()function: