0

How to use js files

How can i use a js file already create in widget?

What does that means?
<script src="myscripts.js" />

Should I create a xml file to import js files?

Any help. Thanks

by
  • fo
  • Nov 17, 2009
10 Replies
  • You have two options that I know of.

    1. Include them from the widget.xml file by using <script src="" />
    2. Use include() from within a .js file.

    What I do is, within my widget.xml file I include the basic platform and framework files. Then I include an init.js file which contains all the script files I want to include, using the secong form.

    Widget.xml file:

    CODE
    <?xml version="1.0" encoding="UTF-8"?>
    <widget>
    <!-- Differs for each OEM device and provides device-specific implementations. -->
    <script src="Platform/platform.js"/>

    <!-- The Yahoo! Widget Framework -->
    <script src="Framework/kontx/1.2/src/all.js"/>

    <!-- Application starting point -->
    <script src="Javascript/init.js"/>
    </widget>


    init.js file:

    CODE
    include("Javascript/core/Preferences.js");
    include("Javascript/core/API.js");
    ...

    KONtx.application.init({
    views : [{
    id : 'ViewId',
    viewClass : View
    }],
    defaultViewId : 'DefaultView',
    settingsViewId : 'SettingView'
    });
    0
  • QUOTE (Steve @ Nov 17 2009, 08:02 AM) <{POST_SNAPBACK}>
    You have two options that I know of.

    1. Include them from the widget.xml file by using <script src="" />
    2. Use include() from within a .js file.

    What I do is, within my widget.xml file I include the basic platform and framework files. Then I include an init.js file which contains all the script files I want to include, using the secong form.

    Widget.xml file:

    CODE
    <?xml version="1.0" encoding="UTF-8"?>
    <widget>
    <!-- Differs for each OEM device and provides device-specific implementations. -->
    <script src="Platform/platform.js"/>

    <!-- The Yahoo! Widget Framework -->
    <script src="Framework/kontx/1.2/src/all.js"/>

    <!-- Application starting point -->
    <script src="Javascript/init.js"/>
    </widget>


    init.js file:

    CODE
    include("Javascript/core/Preferences.js");
    include("Javascript/core/API.js");
    ...

    KONtx.application.init({
    views : [{
    id : 'ViewId',
    viewClass : View
    }],
    defaultViewId : 'DefaultView',
    settingsViewId : 'SettingView'
    });



    Thanks for your reply.

    So, the js files should be "include" in the init.js file.

    Now, I have a js file in which there are several functions. and it is "include" in the init.js. I want to know how can I use these functions in other js files.

    Thanks
    0
  • QUOTE (shiyi @ Nov 17 2009, 08:22 AM) <{POST_SNAPBACK}>
    Thanks for your reply.

    So, the js files should be "include" in the init.js file.

    Now, I have a js file in which there are several functions. and it is "include" in the init.js. I want to know how can I use these functions in other js files.

    Thanks

    You can either "include" them in the init.js file or use the first option I mentioned where you include them in the widget.xml file using <script src="" />. Either one works, it's up to you how you want to structure your app.

    Once the files are included, the functions should be available within any view of your app. They are loaded into global space, and can be callable by the function name/variable.
    0
  • QUOTE (Steve @ Nov 17 2009, 08:31 AM) <{POST_SNAPBACK}>
    You can either "include" them in the init.js file or use the first option I mentioned where you include them in the widget.xml file using <script src="" />. Either one works, it's up to you how you want to structure your app.

    Once the files are included, the functions should be available within any view of your app. They are loaded into global space, and can be callable by the function name/variable.


    I create a js file test.js. There is a function:

    var test = function(){ print("#### TEST!");};

    and it is included in init.js.

    so how can i call this function ? I was tried to call it in my udateView like:

    this;

    with no success.
    I think i was wrong called. but i do not know how. Thanks for advise.
    0
  • QUOTE (shiyi @ Nov 17 2009, 08:55 AM) <{POST_SNAPBACK}>
    I create a js file test.js. There is a function:

    var test = function(){ print("#### TEST!");};

    and it is included in init.js.

    so how can i call this function ? I was tried to call it in my udateView like:

    this;

    with no success.
    I think i was wrong called. but i do not know how. Thanks for advise.


    The updateView is like this:

    updateView: function(){
    test;
    }

    I made a mistake.
    0
  • QUOTE (shiyi @ Nov 17 2009, 08:58 AM) <{POST_SNAPBACK}>
    The updateView is like this:

    updateView: function(){
    test;
    }

    I made a mistake.

    Is everything working now?
    0
  • QUOTE (Steve @ Nov 17 2009, 09:03 AM) <{POST_SNAPBACK}>
    Is everything working now?


    NO, it doesn't work. and there is no error.
    It seems that the function doesn't exist.
    Am I called wrong?

    Thanks
    0
  • QUOTE (shiyi @ Nov 17 2009, 09:17 AM) <{POST_SNAPBACK}>
    NO, it doesn't work. and there is no error.
    It seems that the function doesn't exist.
    Am I called wrong?

    Thanks

    You need to include parenthesis when calling test. Use tes() instead of test.
    0
  • QUOTE (Steve @ Nov 17 2009, 09:23 AM) <{POST_SNAPBACK}>
    You need to include parenthesis when calling test. Use tes() instead of test.


    So foolish!

    It works. Thanks a lot!
    0
  • QUOTE (shiyi @ Nov 17 2009, 08:55 AM) <{POST_SNAPBACK}>
    I create a js file test.js. There is a function:

    var test = function(){ print("#### TEST!");};

    and it is included in init.js.

    so how can i call this function ? I was tried to call it in my udateView like:

    this;

    with no success.
    I think i was wrong called. but i do not know how. Thanks for advise.

    When I mentioned the widget.xml file above, I actually meant the main.TV file instead. Sorry. So your going to want to include <script src="Javascript/init.js"/> in your main.TV.
    0

Recent Posts

in General - Yahoo! TV Widgets