admin管理员组文章数量:1356294
I've a problem that is a bit tricky over here, I'm trying to apply a simple JQuery line of code that addClass
to a div
with class pop-up
but the problem is that class pop_up
is not accessible after jQuery(document).ready(function($){});
This class is actually added from an external JS
and the pop_up
functionality
is also added from an external JS
so I'm wondering
How To Add The Class using JQuery after the external JS get executed so pop_up class can be found using:
$('.pop_up');
What I've tried:
jQuery(document).ready(function($) {
$('.pop_up').addClass('importantRule');
$('.pop_up').toggleClass('importantRule');
});
this is not working as the external JS
added the class somehow after .ready
, so if you tried to print out $('.pop_up')
it will be undefined.
I've also tried to look for the class using a constant class container of div.pop_up
like this:
$('div.element').find('.pop_up').addClass('importantRule');
that didn't work either, I know for a fact the problem is with calling the function in .ready
as some how the external JS
get executed after it so,
is there away around this?
if not, is there a way to detect if all of external JS files are ready and loaded?
I've a problem that is a bit tricky over here, I'm trying to apply a simple JQuery line of code that addClass
to a div
with class pop-up
but the problem is that class pop_up
is not accessible after jQuery(document).ready(function($){});
This class is actually added from an external JS
and the pop_up
functionality
is also added from an external JS
so I'm wondering
How To Add The Class using JQuery after the external JS get executed so pop_up class can be found using:
$('.pop_up');
What I've tried:
jQuery(document).ready(function($) {
$('.pop_up').addClass('importantRule');
$('.pop_up').toggleClass('importantRule');
});
this is not working as the external JS
added the class somehow after .ready
, so if you tried to print out $('.pop_up')
it will be undefined.
I've also tried to look for the class using a constant class container of div.pop_up
like this:
$('div.element').find('.pop_up').addClass('importantRule');
that didn't work either, I know for a fact the problem is with calling the function in .ready
as some how the external JS
get executed after it so,
is there away around this?
if not, is there a way to detect if all of external JS files are ready and loaded?
Share Improve this question edited May 10, 2016 at 12:27 BenG 15.2k5 gold badges47 silver badges62 bronze badges asked May 10, 2016 at 11:50 Ahmad Al-SanieAhmad Al-Sanie 3,7852 gold badges26 silver badges59 bronze badges 4- I believe this approach will work: Use a time out script and do the essential stuff in this script. Then the rest can execute. Also, have a look at this. Might help. – Satej S Commented May 10, 2016 at 11:54
- When does this other code add the "pop_up" class? On load or some other time? And which code es first? – Mario Plantosar Commented May 10, 2016 at 11:55
- Just include your jquery code after you've included the external script – Velimir Tchatchevsky Commented May 10, 2016 at 11:55
- Do you have any access to the external JS? If so, you could trigger an event – pistou Commented May 10, 2016 at 12:09
5 Answers
Reset to default 4You can have $(document).ready() multiple times in a page. The code gets run in the sequence in which it appears.
You can use the $(window).load()
event for your code since this happens after the page is fully loaded and all the code in the various $(document).ready()
handlers have finished running.
$(window).load(function(){
//your code here
});
One way is to use a setTimeout
to check:-
function checkForElement() {
setTimeout(function() {
if ($('.pop_up').length) {
$('.pop_up').addClass('importantRule');
} else {
checkForElement();
}
}, 100);
}
checkForElement();
This will wait for 100ms, then check. if its not there then it will wait again, and again, etc.
You experience a race condition. If the external script finishes running first, then your code will work, but if your code finishes first, then it breaks.
You can either hack around it like Satej S suggested on the ments, and give a reasonable timeout which will make sure the external script finished running, and after that timeout, run your script.
setTimeout(function(){ doSomething(); }, 3000);
A better solution will be using a callback from the external script (can you edit the external script?). This way, the second the external script ends, it calls for one of your internal functions that will start working.
To avoid timeouts that most other answers suggest, I would advise you to try adding "defer" tag to your script include. That way the script trying to add class "importantRule" will wait until all of the other scripts have been loaded and executed.
In addition, I found this question quite similar load and execute order of scripts
Hope this helps! Thanks
本文标签: jqueryLoading your JavaScript after an external JS get executedStack Overflow
版权声明:本文标题:jquery - Loading your JavaScript after an external JS get executed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744057175a2583466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论