views:

96

answers:

6

Hello,

Can I load javascript code using <link> tag in my website ?

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

Can I make the popup window appear using:

<link href="test.js"></link>

Thanks

+1  A: 

JavaScript code would generally be loaded using a script tag, like so:

<script type="text/javascript" src="test.js"></script>
Syntactic
+2  A: 

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

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

The end tag must be the full </script>, don't abbreviate the way 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 the uses of the <link> tag, see w3.org.

Jonathon
+1 for the hint that abbreviating the end tag won't work.
stakx
+1  A: 

No. A Link tag like is for CSS files or for relational links (like next).

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

<script language="javascript" src="file.js" />
Oded
A: 

I know how to load javascript files. But the question is if there is any alternative way to load javascript like using the link tag

john terry
Please comment on individual answers, or edit your question. It's very difficult to follow a discussion if you're adding comments by answering your own question.
Syntactic
A: 

Using jquery/javascript you can hook into the click event on the href element. You can then do any processing you need.

Jay
+5  A: 

No. There was a proposal to allow:

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

analogously to stylesheets. This is even quoted as an example in the HTML 4 DTD, but browser implementation never happened. Shame, as this would have been much cleaner.

bobince
+1 interesting reference.
Jonathon