global jquery function
I have to write global function in js file that is loaded initially. I want to write function on it so that it can b开发者_开发技巧e accessed from all pages. I am new in jquery. I want to know how to write function in js file and call it form other pages?
You can add your own jQuery function by doing the following
$.fn.MyFunction = function()
{
//Code here
}
Then inside of another script or the same script, you can run your function by doing $('#myId').MyFunction();
. This is for running the function on an object. The suggestion below is for just running a function.
Adding it to the jQuery object itself:
$.MyFunction = function()
{
//Code here
}
Then call it as $.MyFunction()
You can define the functions in js file and just include these js files in the pages where you want to use it using script tag.
All the functions defined in the js files will become global and can be used anywhere on the page or in other js files.
<script src="scriptFileName1.js" type="text/javascript"></script>
<script src="scriptFileName2.js" type="text/javascript"></script>
精彩评论