Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calling function in external js file
Message
From
30/11/2015 02:50:45
 
General information
Forum:
Javascript
Category:
Other
Miscellaneous
Thread ID:
01628030
Message ID:
01628049
Views:
34
>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');
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform