Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Can't I Use JS Function Called 'evaluate' in HTML?

I'm kind of curious why this doesn't work

JavaScript:

function evaluate(){
    console.log(42);
}

HTML:

<a onclick="evaluate()">Click Me!</a>

Is evaluate a reserved keyword somewhere on the html side?

like image 774
Dan Avatar asked Feb 07 '13 16:02

Dan


People also ask

What is wrong with eval ()?

Calling eval() will be slower than using alternatives, because it has to call JavaScript interpreter, which will convert evaluated code to the machine language. That means if you run the code more than once, the browser will have to interpret the same code again, which is highly inefficient.

How do you evaluate in JavaScript?

eval() is a function property of the global object. The argument of the eval() function is a string. It will evaluate the source string as a script body, which means both statements and expressions are allowed. It returns the completion value of the code.

How do you call js function from another js file in HTML?

Calling a function using external JavaScript file Js) extension. Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function.

How do I execute a JavaScript function?

Use the keyword function followed by the name of the function. After the function name, open and close parentheses. After parenthesis, open and close curly braces. Within curly braces, write your lines of code.


2 Answers

document.evaluate is needed for parsing XMLs, see the reference in the MDN here.

like image 186
Uooo Avatar answered Oct 15 '22 20:10

Uooo


Evaluate is not a reserved word in JavaScript, document.evaluate is used to evaluate XPath expressions.

You could still name your function evaluate if you used a less obtrusive method of attaching your event handler:

var evaluate = function (){
    console.log(42);
}

document.addEventListener('click', evaluate, false);

Example

like image 44
Useless Code Avatar answered Oct 15 '22 19:10

Useless Code