Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between using link and script tag to reference JavaScript source?

Tags:

javascript

I've tried using both the following source-reference-lines. They both compile. But what is the difference?

1st method:

<script src="~/Scripts/jquery-1.4.1.js" type="text/javascript"></script> 

2nd method:

<link href="~/Scripts/jquery-1.4.1.js" type="text/javascript" /> 

Note: There's also the similar Difference between script and link as="script" tags which asks about <link href="js/script.js" as="script">, which is different.

like image 811
Aske B. Avatar asked Aug 29 '12 12:08

Aske B.


People also ask

What is script tag in JavaScript?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

Does JavaScript need script tag?

JavaScript became the default language for HTML5 and modern browsers. Therefore, now adding text/javascript isn't required in <script> tag.

Which is better option for JavaScript external link in head or body?

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.

What tag is used for linking a JavaScript file to HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.


1 Answers

link tag is used to relate stylesheets or any other linked documents instead of including javascript files.

The HTML Link Element <link> specifies relationships between the current document and other documents. Possible uses for this element include defining a relational framework for navigation and linking the document to a style sheet.

rel Attribute:

This attribute names a relationship of the linked document to the current document. The attribute must be a space-separated list of the link types values. The most common use of this attribute is to specify a link to an external style sheet: the rel attribute is set to stylesheet, and the href attribute is set to the URL of an external style sheet to format the page. WebTV also supports the use of the value next for rel to preload the next page in a document series.

Possible Values:

  • alternate - An alternate version of the document (i.e. print page, translated or mirror)

  • stylesheet - An external style sheet for the document

  • start - The first document in a selection

  • next - The next document in a selection

  • prev - The previous document in a selection

  • contents - A table of contents for the document

  • index - An index for the document

  • glossary - A glossary (explanation) of words used in the document

  • copyright - A document containing copyright information

  • chapter - A chapter of the document

  • section - A section of the document

  • subsection - A subsection of the document

  • appendix An appendix for the document

  • help A help document

  • bookmark A related document

  • shortcut icon A related (favorite icon) image of the document

While The HTML Script Element <script> is used to embed or reference an executable script within an HTML or XHTML document.

like image 95
Ahsan Khurshid Avatar answered Sep 28 '22 01:09

Ahsan Khurshid