When I call my JavaScript function B, the JavaScript console in Firefox said that function A is not defined, but on Chrome browser it's defined. And when I call function "A" in body segment:
<input type="button" onclick="A()" value=" ..A.. ">
Firefox said that function B is not defined. Why?
<html>
<head>
<script language="javascript" type="text/javascript">
function B() {
alert(" hi B ");
document.write('<br><br><input type="button" onClick="A()" value=" ..A..">');
};
function A() {
alert(" hi A");
document.write('<br><br><input type="button" onclick="B()" value=" ..b..">');
if (window.WebCL == undefined) {
alert("Unfortunately your system does not support WebCL. ");
return false;
}
}
</script>
</head>
<body>
<input type="button" onclick="B()" value=" ..B.. ">
</body>
</html>
A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .
To clear a text input, assign blank string to 'value' attribute (not innerHTML attribute) document . getElementById("text"). value = ""; And don't call myfunction in JS tab If you use a <form> then an input with 'type' attribute "reset" will do the same thing.
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)
A function is not defined or is undefined if the value to be inputted is not in its domain. For instance, the domain of the function f(x)=√x f ( x ) = x is x≥0 x ≥ 0 . Getting its value at x=−2 is impossible as it is not defined at this point, i.e., it is not in its domain.
The first write clears the content of the document, resulting in function A being undefined
The problem is that you are calling document.write
after the page has loaded, which effectively wipes out the existing content of the page, including the embedded script. You should be using DOM manipulation methods instead.
Try this one:
<html>
<head>
<script language="javascript" type="text/javascript">
var A = function() {
alert(" hi A");
document.write('<br><br><input type="button" onclick="B()" value=" ..b..">');
if (window.WebCL == undefined) {
alert("Unfortunately your system does not support WebCL. ");
return false;
}
}
function B(){
alert(" hi B ");
document.body.innerHTML = ('<br><br><input type="button" onClick="new A()" value=" ..A..">');
}
</script>
</head>
<body>
<input type="button" onclick="B()" value=" ..B.. ">
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With