This is a migrated thread and some comments may be shown as answers.

Calling javascript functions from separate file

1 Answer 519 Views
Show your code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Anupam
Top achievements
Rank 1
Anupam asked on 07 Dec 2014, 01:45 PM
Hello. I have created a Kendo UI core widget project. I have created a viewmodel source code as home.js. want to create a separate javascript functions file containing some common functions required for app. How can I refer tghe functions of these files in home.js.

Please advice.

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 10 Dec 2014, 04:16 PM
Hello Anupam,

In order to use your common functions in home.js you need to make sure that you link the common JavaScript file in the app, before you link the home.js file. For instance if you have a common.js containing the following code snippet:
var Common = {
    sayHello: function() {
        alert("Hello!");
    }
}
 
function sayGoodbye(){
    alert("Bye!");
}

as long as you link this file before the home.js file in index.html:
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="kendo/styles/kendo.mobile.all.min.css" rel="stylesheet" />
    <link href="styles/main.css" rel="stylesheet" />
 
    <script src="cordova.js"></script>
    <script src="kendo/js/jquery.min.js"></script>
    <script src="kendo/js/kendo.mobile.min.js"></script>
 
    <script src="scripts/common.js"></script>
    <script src="scripts/home.js"></script>
</head>

You can invoke the sayHello() and sayGoodye() functions in home.js like this:
Common.sayHello();
sayGoodbye();

There are multiple tutorials around the web that can help you get started with that. You can also explore the concept of JavaScript namespaces and modules. On that topic you can also find multiple tutorials around the net.

Regards,
Tina Stancheva
Telerik
 

Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

 
Tags
Show your code
Asked by
Anupam
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or