how to make a global function in jquery, and call it from another loaded page

爷,独闯天下 提交于 2019-12-05 13:05:44

Why can't you put your function in an external .js file and then import it into your main page?

function setGlobalFunc()
{    
   window.myfunc = (function(){alert("o hai");});    
}    

setGlobalFunc();    
myfunc();    

Function created from within another function, still globaly accessible.

No jQuery required at all.

If content is loaded dynamically, events need to be hooked up using the .live() method

$("#mybutton").live("click", function() { /* code here */ });

The problem is that you chain the function as if it is part of jQuery, but you have not made it be a plugin to the jQuery.

you should call it like this

$("#mybutton").click(function(){
    // var $bottomLineBtn = $(this);
    // $bottomLineBtn.parent();
    myfun('2','43234');
})

or define your function as a plugin of jquery like

 $.fn.myfun = function(tab_index, acc_id){
    alert(tab_index +" | +" acc_id);
 };

and call it as you currently do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!