First, understand one thing. If you define a JS function and it cannot be used, there are only two possibilities.
First, you encountered an error in introducing the JSP page and introduced the sequence.
Second, there is a problem with the scope.
You also need to know when the JS you write will be loaded in.
[Javascript] view plaincopy
<Script>
Function testAli1 () {alert ("testAli1 ");}
[Javascript] view plaincopy
Var chen = {};
$ (Document). ready (function (){
Function testAli2 () {alert ("testAli2 ");}
Chen. testAli3 = function () {alert ("testAli3 ");};
});
;/Script>
The testAli1 () method is loaded in the document loading process. That is to say, the document is executed from top to bottom and the execution time is automatically loaded in.
If it is written in the ready method, it will load these items after all the documents are loaded.
Therefore, writing in is more secure.
However, it will certainly cause new problems. The testAli1 () method is a global method, and you can call it wherever you are.
TestAli2 () is an absolute internal method. You can only call it in ready. It is impossible to call it elsewhere.
If you want to call the above method anywhere, you need to add something like this namespace, and add the testAli3 () method below to call it.
And the calling rule is chen. testAli3 (). If you feel this way, you can write window. testAli3 () directly in it, which is equivalent to a global one.
Example:
(1) <a href = "javascript: void (0)" onclick = "testAli1 ()"> Test 1 </>
(2) <a href = "javascript: void (0)" onclick = "testAli2 ()"> Test 2 </>
(3) <a href = "javascript: void (0)" onclick = "testAli3 ()"> Test 3 </>
(1) normal call (2) Error testAli2 not defined (3) normal call
Therefore, the issue that cannot be executed in JS is that you have written an error in the loading sequence. This can be a 1.1-point test, which is very important.
In addition, you can call the methods in the internal scope.
Appendix:
When using Jquery, you cannot avoid using the ready function. In fact, this ready function is not only used to load the corresponding js script after the file is loaded, to ensure that some elements already exist in HTML documents before js scripts are used (to avoid some errors ). In fact, the ready function also has an internal field which is invisible to the outside. However, if you want to define a function in the ready scope, it can also be called externally. You just need to set it like this:
Window. funname = function () {// do something };