jasvascript call functions from different scripts

56 views Asked by At

I have two different script tags,can i call function from second..like this

<script>
var = something;
. . .
<td onclick="myFunc("+something+")"></td>
</script> 

<script>
  function myFunc(data){..}
</script>
2

There are 2 answers

0
Sergey Maksimenko On

Move your <td> tag out of <script> tag down after second <script> and you will be able to.

Btw, consider changing blockquotes in <td onclick="myFunc("+something+")"></td> to quotes like this <td onclick="myFunc('+something+')"></td>, other way your HTML will not be parsed correctly

2
Cymen On

Yes, you can. The functions are at the global level. Here is an example:

<script type="text/javascript">
    function hi() {
      alert('hi');
    }
</script>

<script type="text/javascript">
hi();
</script>

This does indeed result in a popup with "hi".

JSFiddle: http://jsfiddle.net/we8pb7md/