Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Calling function in external js file
Message
 
 
À
30/11/2015 02:50:45
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01628030
Message ID:
01628056
Vues:
26
>>This must be super newbie question.
>>
>>If I create a javascript function in my page javascript, like the following:
>>
>>function TestMsg()
>>{
>>  alert('Test Message');
>>}
>>
>>everything works. In the javascript, before the above function is written, I can call:
>>
>>TestMsg();
>>
>>and Alert shows the message.
>>
>>But if I move the above function to an external .js file (e.g. dmitry.js) as follows:
>>
>><script type="text/javascript">
>>function TestMsg() {
>>  alert('test message');
>>}
>></script>
>>
>>I check in the Chrome that the file dmitry.js is loaded. But when I call TestMsg(); in the javascript, nothing alerts.
>>
>>What am I missing?
>
>Unless you've swapped the code snippets above then, in the external file, the function should *not* be enclosed in a 'script' tag. i.e. this should work:
<!DOCTYPE html>
><html xmlns="http://www.w3.org/1999/xhtml">
><head>
>    <title></title>
></head>
><body>
>    <script type="text/javascript" src="dmitry.js"></script>
>    <button onclick="TestMsg()">Click me</button>
></body>
></html>
and Dmitry.js:
function TestMsg() {
>    alert('test message');
>}
Thank you very much. My mistake.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform