0

I encountered the following script in a webpage source :

<script type="text/javascript">
  WebFontConfig = {"typekit":{"id":"cmr1bul"}};
  (function() {
    var wf = document.createElement('script');
    wf.src = 'https://s1.wp.com/wp-content/plugins/custom-fonts/js/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
    })();
</script>

From what I see, this script inserts the content of https://s1.wp.com/wp-content/plugins/custom-fonts/js/webfont.js before the first script in the page. What is the difference between the above and putting

<script type="text/javascript" src="https://s1.wp.com/wp-content/plugins/custom-fonts/js/webfont.js"> </script>

as the first script in the page ? What is gained by using the longer version ?

0

1 Answer 1

2

It's absolutely equivalent to the following code if both script elements are added before any other one:

<script>WebFontConfig = {"typekit":{"id":"cmr1bul"}};</script>
<script src="https://s1.wp.com/wp-content/plugins/custom-fonts/js/webfont.js" async></script>

...or:

<script src="https://s1.wp.com/wp-content/plugins/custom-fonts/js/webfont.js" async></script>
<script>WebFontConfig = {"typekit":{"id":"cmr1bul"}};</script>

...because the script is asynchronously included...

Sign up to request clarification or add additional context in comments.

3 Comments

@RokoC.Buljan That's why I'm telling the OP that's just before adding them before. Am I mistaken? :D
Got it : the advantage of the longer version is that it may be put anywhere in the page, it will always result in the webfont.js being put first.
@EwanDelanoy Yeah... BTW unless the script must be really added this way, I suggest you to use script elements..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.