admin管理员组

文章数量:1410697

I have multiple external javascript files linked to my html page and want to call a function in one of them. Right now I'm doing onClick = "func()", but it's not calling the function. I'm trying to figure out the problem.....Do I need to specify which script the function is in? If so, how do I do that? Thanks.

Here is code:

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

<li id="tab"><span onClick = "get_code('tabOne')">Tab</span></li>
...

In file1.js:

function get_code(str)
{
    alert(str);
}

I have multiple external javascript files linked to my html page and want to call a function in one of them. Right now I'm doing onClick = "func()", but it's not calling the function. I'm trying to figure out the problem.....Do I need to specify which script the function is in? If so, how do I do that? Thanks.

Here is code:

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

<li id="tab"><span onClick = "get_code('tabOne')">Tab</span></li>
...

In file1.js:

function get_code(str)
{
    alert(str);
}
Share Improve this question edited Jun 30, 2011 at 20:17 demongolem 9,71636 gold badges97 silver badges105 bronze badges asked Mar 18, 2011 at 22:52 rachrach 6916 gold badges16 silver badges20 bronze badges 1
  • 1 Can you post the relevant JavaScript and HTML? – Ewan Heming Commented Mar 18, 2011 at 22:55
Add a ment  | 

3 Answers 3

Reset to default 1

By not working, do you mean that you do not get an alert box at all? or is the alert not displaying what you want? If it's not displaying what you want, it could be because your code alert(st); is wrong, it should be alert(str);.

Do onmouseover="" or onclick="" show the dialog? Javascript is case-sensitiv.

If you try to copy the file.js function and paste it in <HEAD> section of HTML, does that work? Also, you should place <script type="text/javascript" src="file1.js"></script> in the <HEAD> section of your HTML. Check for any variable name clashes. Also, in the alert function, you are passing st wherein it should be str. Make sure your Javascript path is correct. Right now, javascript files should be in the same directory. How about trying out

<li id="tab" onClick="get_code('tabOne');">Tab</li>

本文标签: htmlmultiple external javascript files how to call functionStack Overflow