Questions About Using JavaScript and HTML Event Handlers

When you use events in your HTML and you include a small bit of JavaScript, what is called? It is called a "JavaScript attribute function", just a "JavaScript attribute" or what? eg:

<button onClick="location.href='some_place'">click me</button>

When using events, and you use the keyword "return", what is called? Is there any specific terminology that is used to describe this? I know what he does and how it works, I just don’t know what to call it. eg:

<button onClick="return someFunction();">click me</button>

Can these two JavaScript fragments be combined into one in this attribute? I would like to combine the first two examples in one. When the button is pressed, I want to call a JavaScript function. If the function returns true, I want location.href to start. If the function returns false, I do not want location.href to run.

+4
source share
3 answers

onclick- the event handler   returnis the return statement after the function reaches the return statement, it will stop execution

combining two functions might look like this:

function  someFunction(){
  var x = someBool; 
  if(x){
     location.href='some_place'
   }else{
     return false;
  }

}
0
source

onclick - HTML, . - . , , , " , HTML , .

, :

<button onclick="if(someFunction()) location.href='some_place';"></button>

, , . a href:

<a href="some_place" onclick="return someFunction();"></a>

someFunction false, click , .

, :

onclick="return someOtherFunction()"

someOtherFunction .
JS:

document.getElementById("the_id_of_the_element").onclick=function() {/* your code here */};

( ):

function bindEvent(element, type, handler) {
    if(element.addEventListener) {
        element.addEventListener(type, handler, false);
    } else {
        element.attachEvent('on'+type, handler);
    }
}
bindEvent(document.getElementById("the_id_of_the_element"), 'click', function() {/*Your code here*/ });

jQuery ,

$("#the_id_of_the_element").on("click", function() {/*your code here*/});
$("#the_id_of_the_element").on("click", function() {/*some other code*/});
//both will execute
0
  • JavaScript, .
  • , , JavaScript. , , , false.
  • .

# 3 , , , undefined, , , . true, .

0

Source: https://habr.com/ru/post/1543456/


All Articles