Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my JavaScript function "a" not defined?

Tags:

javascript

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>
like image 691
al3x609 Avatar asked Apr 07 '12 16:04

al3x609


People also ask

Why is my JavaScript function undefined?

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 .

How do you fix function is not defined?

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.

How do you define a function in JavaScript?

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, ...)

What does it mean when a function is not defined?

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.


Video Answer


3 Answers

The first write clears the content of the document, resulting in function A being undefined

like image 185
stefan bachert Avatar answered Oct 25 '22 16:10

stefan bachert


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.

like image 31
Cheran Shunmugavel Avatar answered Oct 25 '22 16:10

Cheran Shunmugavel


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>
like image 30
Ehsan Khodarahmi Avatar answered Oct 25 '22 17:10

Ehsan Khodarahmi