これを試して:
HTML上:
<a href='some/url/?with¶ms' onclick="if (confirm('Do you wanna submit?')) return MyValidation.actionSubmit();">Submit</a>
<script type="text/javascript" src="myValidation.js" />
myValidation.js
この回答では、jsonとjavascriptの クロージャを使用しました。
; //starts with this to close other js not yet closed
var MyValidation = {
actionSubmit : function() {
//your code go here
//example of calling another function
MyValidation.anotherFunction();
//example of accessing a global variable (for your validation)
MyValidation.variable;
//example of declaring local variable
var word = "hello";
//example of calling another function with parameters
MyValidation.anotherFunctionWithParameters(word, MyValidation.variable);
//the returning
return MyValidation.someValidation(word);
}
//Yes, this is a comma.
//I'm putting in the beginning so we see this and didn't forget :)
,anotherFunction : function() {
//some code
//maybe a return
}
,variable : {}
,anotherFunctionWithParameters : function(param1, param2) {
//more code, maybe a return
}
,someValidation : function(parameter) {
//some validation
return true|false;
}
};
これを見てください:
- JavaScriptクロージャはどのように機能しますか?
- json