+ 1

Jquerry problem

lets say that i have main.js like this main.js $(function(){ function alertHi(){ alert("hi"); } alertHi(); }); everything works fine but let say that i created a second.js file which i wanna call alertHi(); second.js alertHi(); -> gives error because function declaration is in a function so its local i understand that if i put the declaration outside the $(function(){}) it would work but i need it inside

27th Jun 2018, 5:45 PM
Eren
Eren - avatar
4 Réponses
+ 1
i used window.functionname = function
27th Jun 2018, 8:44 PM
Eren
Eren - avatar
+ 3
You can solve your problem with a little extra work sencond.js- $.getScript("main.js",function(True){}); main.js- $(function(x){ if(x){ alertHi(); } function alertHi(){ alert("hi"); } });
27th Jun 2018, 8:18 PM
ODLNT
ODLNT - avatar
+ 1
AshBreez450 look at my last statement i said that i need it inside
27th Jun 2018, 8:43 PM
Eren
Eren - avatar
- 1
If you do put the function "alertHi()" outside the "$(function(){})", you can call "alertHi" inside and be fine
27th Jun 2018, 6:42 PM
AshBreez450
AshBreez450 - avatar