简体   繁体   中英

In Windows 8 Metro-Style App Development, how can I use objects from external JS files/libraries?

I have a JavaScript calendar in a .js file, which I used in an html page and I'm trying to use it on a metro app. How can I import it and call from it in the data.js default file? (The data.js file starts with an "(") And then how can I create a calendar Element to use on the "element" field of the tile item? If I manage to import into the data.js file, and create an element using createElement function suggested by Microsoft, will it work properly, since it requires that external JavaScript file? If not, what can I do?

Add the file to the Project in visual studio, the include the JS file script tag in the default.html of your application. You can then use it like any other JS library.

Also, two big things to keep in mind. If possible try to include the .js source file in your project (like you are doing). Calling out to a CDN or external hosted location will flag you for some security checks you wouldn't normally have to go through.

There is no guarantee the JavaScript library will continue to work over time either. I have kept older versions of SoundJS and jQuery around for this reason. I've seen apps throw some errors when newer versions were doing something like DOM Manipulation that flagged some security violations.

In addition to referencing the script in your HTML page, like so:

<script src="/js/calendar.js"></script>

you can drag the file from Solution Explorer and drop it at the top of the .js file where you want to use it, and it will provide you with a reference that looks like this:

/// <reference path="calendar.js" />
(function () {
    "use strict";

Once you have that reference, Visual Studio will give you nice Intellisense for the .js file when you reference its functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM