-2

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 Answers2

0

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

0

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/

Cymen
  • 14,079
  • 4
  • 52
  • 72
  • but can i use variables in those functions? variables declared in first,used in second? – misko miskovic Jun 19 '15 at 21:09
  • @miskomiskovic No, not unless you make them globals by omitting the `var` -- if you make them globals, they'll work. But it is bad practice -- it is good idea to use CommonJS or some other module system but it requires tooling/time/setup. – Cymen Jun 19 '15 at 21:45