I know that doing this is possible:
$(document).ready(testing);
function testing(){
alert('hellow world!');
}
But how would I make something like this work where I want to pass a variable to the function:
$(document).ready(testing('hey world!'));
function testing(message){
alert(message);
}
You could use Function.prototype.bind
but it come with some disadvantages like losing the this
reference or the Event
object.
$(document).ready(testing.bind(null, 'message')); //First parameter == this;
function testing(msg){
alert(msg); //Alert message
}
or you can do it like this :
$(document).ready(testing.bind('message'));
function testing(){
alert(this); //Alert message
}
Fiddle : http://jsfiddle.net/KedKq/1/
You can use an anonymous function:
$(document).ready(function() {
testing('hey world!'));
});
function testing(message){
alert(message);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.