r/JavaScriptHelp • u/spod_zilla • Nov 04 '21
❔ Unanswered ❔ (New to JS) Help With Delaying a Script
Hi,
I've been tasked with adding the below script to a clients website to load an online booking widget, however it's absolutely tanking the page load speed.
I was hoping there is a way to stop the script loading until a button is clicked, or at least delay the loading of the script to allow the page to load.
<script type='text/javascript'>!function(b,c){b.saloniqid='119423d7-1209-4ee5-8e1b-f8a71f0b7345',b.saloniqsite='https://bookings.saloniq.co.uk';var d,g,e='onlinebookingwidget-loader',f=c.getElementsByTagName('script')[0];c.getElementById(e)||(d=c.createElement('script'),d.id=e,d.async=!0,d.src=b.saloniqsite+'/scripts/onlinebookingwidget-loader.js?d='+Date.now(),g=f?f.parentNode:c.getElementsByTagName('head')[0],g.insertBefore(d,f))}(window,document);</script>
Thanks!
2
u/besthelloworld Nov 04 '21
Try <script defer type...
However, there's still going to be a chunk of time where the page is non interactive but you can at least make it after paint.
1
u/paganaye Nov 04 '21
you can delay the start of any function in javascript with setTimeOut.if what you do within the function can be delayed then you'll get more reponsiveness.