Home > Web Front-end > JS Tutorial > JavaScript execution method for uncertain functions_javascript skills

JavaScript execution method for uncertain functions_javascript skills

WBOY
Release: 2016-05-16 15:25:21
Original
1652 people have browsed it

This article analyzes the execution method of javascript for uncertain functions through examples. Share it with everyone for your reference, the details are as follows:

In JavaScript, sometimes we only know the name of a function, but we are not sure whether the function exists. How to determine whether the function exists and execute it. One method is to use eval() to perform concatenation of program strings, but this may cause performance problems. Another method is to use symbolic properties to access functions, because functions are properties of the window object.

Use window[function name] to represent the function object, and use window[function name]() to execute or call the function.

Example:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title>新建网页 1</title> 
</head> 
<body> 
<script language="javascript"> 
function input1_onChange(){ 
  alert('input1_onChage executed.'); 
} 
var objId = 'input1'; 
if(window[objId +'_onChange']){ 
  alert('There is the funtion'); 
}else{ 
  alert('There is not the funtion'); 
} 
if(window[objId+'_onChange']  
  && typeof(window[objId+'_onChange'])=='function'){ 
  window[objId+'_onChange'](); 
} 
var fun = window[objId+'_onChange']; 
if(fun && typeof(fun)=='function'){ 
  fun(); 
} 
</script> 
</body> 
</html> 

Copy after login

I hope this article will be helpful to everyone in JavaScript programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template