admin管理员组文章数量:1415119
I'm new to JS and I'm still trying to understand it all. So I've decided to give myself a few projects so I can learn the syntax.
I have a chat widget that loads when someone lands on my website. However, I'd like to only run this widget after the page has loaded to improve website performance.
How do I go about adjusting this script to run only after my WordPress web page has loaded?
<!--Start of Tawk.to Script-->
<script type="text/javascript">
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='/...../default';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
</script>
<!--End of Tawk.to Script-->
UPDATED: The widget developer suggested this.
<!--Start of Tawk.to Script-->
<script type="text/javascript">
var w = window;
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
function l() {
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='/...../default';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
}
if (w.attachEvent) {
w.attachEvent('onload', l);
} else {
w.addEventListener('load', l, false);
}
})();
</script>
<!--End of Tawk.to Script-->
I'm new to JS and I'm still trying to understand it all. So I've decided to give myself a few projects so I can learn the syntax.
I have a chat widget that loads when someone lands on my website. However, I'd like to only run this widget after the page has loaded to improve website performance.
How do I go about adjusting this script to run only after my WordPress web page has loaded?
<!--Start of Tawk.to Script-->
<script type="text/javascript">
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/...../default';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
</script>
<!--End of Tawk.to Script-->
UPDATED: The widget developer suggested this.
<!--Start of Tawk.to Script-->
<script type="text/javascript">
var w = window;
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
function l() {
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/...../default';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
}
if (w.attachEvent) {
w.attachEvent('onload', l);
} else {
w.addEventListener('load', l, false);
}
})();
</script>
<!--End of Tawk.to Script-->
Share
Improve this question
edited Oct 28, 2020 at 12:41
Wanderlust Consulting
asked Oct 27, 2020 at 18:26
Wanderlust ConsultingWanderlust Consulting
6358 silver badges28 bronze badges
2 Answers
Reset to default 4if you want it to load after the page has parsed add defer to the script tag
<script type="text/javascript" defer>
otherwise to load it after everything else call it on window load :
window.onload = () => {
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/...../default';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
}
If you are using jQuery you can use
$(document).ready(function(){});
To run the javascript after the page has been loaded by the user.
本文标签: Run JavaScript after page load on WordPress using JQueryStack Overflow
版权声明:本文标题:Run JavaScript after page load on WordPress using JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745204298a2647558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论