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

how to declare a global function in jquery, how do I call it from a page loaded in some div on this page using jquery load () function.

the function is simple in the 1st page

  + ----------------------------------------------- +
 |  main links |
 + ----------------------------------------------- |
 |  + ------------------------------------------- + |
 |  | 1st sub page ( myfun function is here) |  |
 |  + ------------------------------------------- + |
 |  |  + --------------------------------------- + |  |
 |  |  |  |  |  |
 |  |  |  |  |  |
 |  |  |  |  |  |
 |  |  |  mybutton clicked myfun called |  |  |
 |  |  |  |  |  |
 |  |  + --------------------------------------- + |  |
 |  + ------------------------------------------- + |
 + ----------------------------------------------- +

But when I click, nothing happens ... here both functions

myfun

myfun(tab_index, acc_id){ alert(tab_index +" | +" acc_id); } 

Mybutton

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

Can someone please help me .. it's too hard to find a solution .. I searched 3 hours ...

============================================= ====== ==================================

updated

here is a detailed script

  • I am working on some dynamic materials using jquery
  • the first page contains 2 divs, those id=menu and the other id=subpage1 ,
  • I click on one link from #menu, the page loads in #subpage1 .
  • page with 2 divs #menu2 and #subpage2
  • here I created a script that automatically loads pages in #subpage2 , taking only 1 parameter .... which is the listmenu identifier.
  • this script is ..

      $ ("# menu2 li"). click (function () {
          $ ("# subpage2"). load ($ (this) .attr ('page_link'));
        }
      }). filter ('# menu2 li: eq (0)'). click ();
     
  • on the loaded page. at the bottom, I use 2 buttons, one button works fine, it calls the function above and changes the value li: eq (1) .. that I did it.

  $ ("# bottom_bar #backbtn"). click (function () {
     var $ bottomLineBtn = $ (this);
     $ bottomLineBtn.parent (). parent (). parent (). find ('# menu2 li: eq (1)'). click ();
     })

... but the other button does not work. I want to call a function that belongs to some #subpage1 in #subpage1 . but cannot access them. I put only the function next to the above function $("#menu2 li").click(function(){ .

  myfun(tab_index, acc_id){ alert(tab_index +" | +" acc_id); } 

but I don’t know how to call this function from the second button loaded in #subpage2

does this script make a scene .. try to understand me .. i'm not very good at describing ...

+4
source share
4 answers

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

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

A function created from another function that is available worldwide.

No jQuery at all.

+4
source

If the content is loaded dynamically, events must be connected using the .live() method

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

The problem is that you bind the function as if it is part of jQuery, but you did not make it a jQuery plugin.

you should call it like this

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

or define your function as a jquery type plugin

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

and name it as you do now

+2
source

All Articles