2

Hi Im trying to call a external JavaScript file called ts.js as follows. Can some one tell me whether the following approach is right or wrong and reasons for your recommendation.

Thanks you

$variable .= '<input type="submit" name="send" id="send" value="Save" onclick="ts.js" />';
1
  • if you want to use something from ts.js just include it in header? Commented Jun 28, 2013 at 9:58

3 Answers 3

5

You need to include the JavaScript file in the markup and then specify a function from it to be executed on click of the input.

So, say, in the <head>:

<script src="ts.js"></script>

And in your construction of the element(s):

$variable .= '<input type="submit" name="send" id="send" value="Save" onclick="functionFromTSJS();" />';
Sign up to request clarification or add additional context in comments.

Comments

2

PHP is a server-side language and you can not call JavaScript from it. Because javascript is a client-side language and actually the browser runs it.

And even if it is possible, on the onClick method, you should use a function not a javascript file.

Javascript files can be included like :

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

After you imported the file like this you can use any functions in it on the onClick.

Comments

0

if you use jQuery you can use

onclick="$.getScript('ts.js');"

with normal javascript I think that you only need to include the javascript in head

var script = document.createElement('script');
script.src = 'ts.js';
document.getElementsByTagName('head')[0].appendChild(script);

you can wrap it with a function like load

function load() {
    var script = document.createElement('script');
    script.src = 'ts.js';
    document.getElementsByTagName('head')[0].appendChild(script);
}

and use

onclick="load('ts.js');"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.