Run JavaScript after page load on WordPress using JQuery - Stack Overflow

I'm new to JS and I'm still trying to understand it all. So I've decided to give myself

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
Add a ment  | 

2 Answers 2

Reset to default 4

if 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745204299a4616511.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信