Calling javascript functions from separate file

2 posts, 0 answers
  1. Anupam
    Anupam avatar
    3 posts
    Member since:
    Nov 2014

    Posted 07 Dec 2014 Link to this post

    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.
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 10 Dec 2014 Link to this post

    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.

     
Back to Top