Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two styles of JavaScript Embed Codes?

This is my first question and I hope I’m doing it right. Couldn’t find any solutions so far, have asked a few coding friends of mine, no sufficient answers yet. Here it goes:

I’m optimizing my side project SaaS, which depends on the users copy/pasting a JavaScript embed code into their Websites. Much like Google Analytics or Twitter require you to do.

When researching how the others do it, I always see one of the two following methods, and I really wonder what’s the difference between them.

Edit for clarification: both loaded scripts sit on external servers.

Method 1: “Standard embed script” like that:

<blockquote id="container">Hello!</blockquote>
<script src="//some.saas.tld/widget.js"></script>

This is what Twitter uses, and it seems to simply load the script which then modifies the <blockquote> element

Method 2: “Hacked apart embed script” like that:

<script> 
  var a = document.createElement('script'); 
  a.src = '//some.saas.tld/widget.js'; 
  parentNode.insertBefore(a); 
</script>

(this is just very basic to show what kind of embedding I mean – it won’t work, and the blockquote isn’t in this, because that’s not the point). Google Analytics does that, for example.

Question is: why do some services use the first method, and why do some use the second? To me, it seems like it’s creating the same results. What’s the PRO/CON?

I’m asking because it’s really tough to make users change the embed code once they have it working within their website’s code. So it’s important to get it right from the start.

Thanks!

like image 894
Teesche Avatar asked Sep 18 '26 08:09

Teesche


1 Answers

The first loading call is basically a synchronous call. This will not allow the next script to load or execute if this is not completed.

However the second manual script loading is asynchronous. It will allow execution and loading of other scripts even after the script is not loaded fully.

There is good article which explain this behavior in details - Checkout the link - https://trevweb.me.uk/javascripthtml-synchronous-and-asynchronous-loading/

like image 61
Ashvin777 Avatar answered Sep 20 '26 22:09

Ashvin777



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!