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
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
JavaScript code would generally be loaded using a script tag, like so:
<script type="text/javascript" src="test.js"></script>
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.
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" />
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
Using jquery/javascript you can hook into the click event on the href element. You can then do any processing you need.
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.