Can I download javascript code using the <link> tag?

Is it possible to download javascript code using the <link> on my website?

For example, I have a javascript file, test.js , which contains simple alert('hello'); code alert('hello');

Is it possible to create a popup using:

 <link href="test.js"></link> 
+21
javascript html css
Apr 13 '10 at 17:14
source share
7 answers

No. It was proposed to allow:

 <link rel="script" href=".../script.js"/> 

similar to style sheets. This is even shown as an example in HTML 4 DTD , but the browser implementation did not succeed. Shame, as it would be much cleaner.

+35
Apr 13 '10 at
source share

You need to use the <script> to include the JavaScript source files:

 <script type="text/javascript" src="mysrc.js"></script> 

The end tag should be complete </script> , do not shorten the way as you can with some tags, as in <script type="text/javascript" src="..."/> .

Yes, alert statements in the included source will appear when they are evaluated by the browser.

For information on using the <link> see w3.org .

+12
Apr 13 '10 at 17:16
source share

Possible justification why not:

  • Items
  • link is only allowed where metadata content is allowed, usually the head , not the body . See Contexts in which this element can be used . All nested elements that enter the body have separate elements for them: img , iframe , etc.

    Items
  • link must be empty, and the script may be non-empty. See: Content Model

Therefore, it is natural to have a separate element for JavaScript, and since we must have a separate element, it is better not to duplicate functionality with link rel="script" .

This theory also explains why img and style have separate elements:

  • img can be placed in the body, so it gets a separate element, although it should be empty.

  • style can be non-empty, so it gets a separate element, although until HTML5 can be placed in the body (now it can through scoped , but still not include external scripts).

+2
Nov 11 '14 at 8:30
source share

JavaScript code is usually loaded using a script tag, for example:

 <script type="text/javascript" src="test.js"></script> 
+1
Apr 13 '10 at 17:15
source share

No. Link tag, as for CSS files, or for relational links (e.g. next ).

This is not a way to load javascript on a page. You need to use the <script> :

 <script language="javascript" src="file.js" /> 
+1
Apr 13 '10 at 17:18
source share

Another option for this is that you can dynamically insert a script file into the current document by creating a script tag, setting your "src" attribute to the script URI, and then inserting it as a child of the HEAD node page.

Performing these steps will cause the browser to get the script file, load it into the document and execute it.

+1
Apr 22 '11 at 19:51
source share

To answer your question directly, no. Not by this method. However, I came across this issue while searching for a similar problem that led me to this issue. After seeing the answers already provided, which for the most part are correct, I went to check the syntax at http://w3schools.com/ . It seems that with HTML5 there is a new attribute for script elements in html.

This new attribute allows you to delay or load javascript files and execute asynchronously (not to be confused with AJAX).

I'm just going to leave a link here and let you familiarize yourself with the details, as it is already available on the Internet.

http://www.w3schools.com/tags/att_script_async.asp

0
Jul 02 '14 at 18:26
source share



All Articles