View Full Version : Calling an external .js file from within the HREF tag
by350
11-10-2002, 03:14 PM
What is the syntax to reference an external .js file within an HREF tag. So basically when you click a button on the page...the href tag around that image will reference this js/script.js file.
Jon Hanlon
11-10-2002, 05:10 PM
Can't be done.
I presume you're talking about having a collection of external functions in, say, cards.js and you want to invoke, say function diamonds().
Can't be done.
All you can do is:
<script language="Javascript" src="cards.js"></script>
<a href="#" onclick="diamonds();return false;">Deal</a>
kdjoergensen
11-10-2002, 07:17 PM
An external javascript library file will be imported and considered 'part' of the page. therefore you do no do anything different compared to an inpage javascript.
If a variable has been declared in an external javascript library you can use this variable later, also in the href property of an a-element, if you like. If a function, as described by Jon, has been declared in the external javascript function you can also use it.
If you are talking about importing a javascript library file only in response to some (inter)action of the user, you would need to use insertAdjacentHTML (msie browsers) or appendChild (DOM capable browsers) to attach the file call to the page:
<a href="javascript: document.body.insertAdjecentHTML('<script language=\"javascript\" src=\"newfile.js\"></script>')">Load newfile library</a>
<a href="javascript: document.body.appendChild('<script language=\"javascript\" src=\"newfile.js\"></script>')">Load new Dom file library</a>
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.