0

I'm curious, usually this is a way I'd call plugin on a page:

<script type="text/javascript" src="plugin.js"></script>
<script>
//apply plugin to elements / call plugin
</script>

but wouldn't it make more sense and perhaps save some lines of code to call plugins like this?

<script type="text/javascript" src="plugin.js">
  //apply plugin / call plugin
</script>

Honestly I don't think I have seen this done before, and I am curious why? I'm new to jQuery and JavaScript so don't judge ;)

6
  • 3
    Huh? Why would you have an empty script block? Did you mean to add src? Commented Apr 17, 2013 at 16:01
  • What's the difference between the two? In the first example all you have is an extra empty script tag, which doesn't do anything. Commented Apr 17, 2013 at 16:01
  • I assume you'd want to load a version of jQuery using src in the first tag Commented Apr 17, 2013 at 16:02
  • @SLaks sorry my mistake, corrected it Commented Apr 17, 2013 at 16:05
  • @Ilya — Did you mean to make the same edit to the second example too? Commented Apr 17, 2013 at 16:05

3 Answers 3

4

<script type="text/javascript"></script> does nothing. It is an empty script element.

Based on your question title, I assume you mean:

<script src="plugin.js"></script>
<script>
// Use functions defined in plugin.js
</script>

compared to:

<script src="plugin.js">
// Use functions defined in plugin.js
</script>

If so: The latter won't work. A script element loads one script which can be from the text node inside the element or a URL.

The specification describes the content of a script element as:

If there is no src attribute, depends on the value of the type attribute, but must match script content restrictions. If there is a src attribute, the element must be either empty or contain only script documentation that also matches script content restrictions.

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

Comments

3

Assuming by your title that you are asking why we don't include the script in the same tag we use the src attribute on.

https://developer.mozilla.org/en-US/docs/HTML/Element/script

src

This attribute specifies the URI of an external script; this can be used as an alternative to embedding a script directly within a document. script elements with an src attribute specified should not have a script embedded within its tags.

This is because browsers may ignore the contained script when you use a src attribute.

Comments

3

What is good practice depends on a lot of things.

If you're going for performance, the absolute fastest way is to have all your scripts in-line and have only just the ones you need.

If you're looking at multiple requests (ie. a user browsing your website) using includes allows you to take advantage of caching.

The other extreme is where you're building a large JavaScript application. Here you might want to load all JavaScript at once, or use some lazy-loader to load modules that you might not need.

There is no one solution for all scenarios. Think about your use case and pick the strategy that is best for you.

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.