4

Is there any advantages of loading a script using HTMLScriptElement instead of loading it by just including it in the DOM?

Maybe for instance it would be easier to keep things clean in the DOM and also hide the scripts (make them less obvious)?

Didn't find any sources on this, that's why I am asking.

5
  • This leads to a discussion more than a programming problem. I think it's to speed up the loading of the page, if the DOM is loaded you can do as you wish but with some things like features you don't mind waiting on can be loaded once the DOM is ready. Rather than slowing it down. But that's just my thoughts on it. I haven't really looked into it so it's more of an assumption than fact. Commented Feb 27, 2016 at 18:31
  • StackOverflow isn't just for problems. It's for general programming questions. What questions can I ask on StackOverflow? Commented Feb 27, 2016 at 18:33
  • I never said you shouldn't be asking, I'm pointing out that you will more than likely get opinion based posted on this. I didn't downvote you did I? I don't need to read what questions can be posted thanks :) Commented Feb 27, 2016 at 18:36
  • @NewToJS Before your edit, your comment was implying that. Sorry if I made any inconvienence. Edit: Also, I did say I couldn't find ANY sources on this, that's why I asked. Commented Feb 27, 2016 at 18:38
  • Before the edit, I hit enter early.... hence adding to the post. Commented Feb 27, 2016 at 18:39

2 Answers 2

1

Normally, you would just include script tags directly in the HTML document. Unless you use the async attribute, subsequent scripts will won't load until the previous ones so you can safely rely on any dependencies to be available.

You could use the HTMLScriptElement interface programmatically to load scripts, if you wanted to keep your HTML cleaner. However, then you'd have to manually create onload and onerror handlers to asynchronously wait for the script(s) to load. This would get messy and complicated unless you build an abstraction around it. And then you're doing something done many times before, see RequireJS, SystemJS et al.

So, wanting to keep your HTML clean of script tags is a reasonable ambition, but you're probably best off looking into an off-the-shelf script loader to do that rather than rolling your own.

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

4 Comments

How can one "use the HTMLScriptElement programmatically to load scripts"?
Well, I mean you would get the interface. In this context document.createElement('script'). If you read between the lines, I think that this is what the OP is getting at. There's a simple example of dynamic script loading on the MDN page he links to
Ha, you might be right. Still it's problematic to confuse createElement with invoking HTMLScriptElement since createElement('script') returns an Element implementing HTMLScriptElement only when called on an HTML document. For instance document.implementation.createDocument(null, 'xml').createElement('script') returns an element NOT implementing HTMLScriptElement.
I think it's kinda obvious that if you call it on an xml dom, rather than a html dom, you're not not going to get the html interface. The context is clearly html
1

There is not really a choice here. HTMLScriptElement is the "interface" exposed by all HTML <script> nodes.

One creates script nodes by calling document.createElement('script') or by passing a <script>...</script> tag string through the HTML parser (this can happen in a variety of ways: from parsing a complete HTML document to setting the innerHTML of an existing element.) When a <script> element is created in an HTML document, HTMLScriptElement is in its prototype chain. Therefore, all properties and methods on HTMLScriptElement are accessible to the <script> element.

HTMLScriptElement is not a constructor function, however. This can be seen by attempting to invoke new HTMLScriptElement(), which throws an Illegal constructor TypeError.

All this is to say that your question does not really make sense, since one cannot load "a script using HTMLScriptElement instead of loading it by just including it in the DOM".

Comments

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.