locked
Dynamically load JS file RRS feed

  • Question

  • Hi -

    script tag is not allowed in Win8. XHR is not the safe way to load a JS file, then IS there a way to load a local JS file in metro app. I do not want to refer in the html file, IS there a way to get it.

    any pointers will help!

    Thanks!

     

     

    Friday, January 6, 2012 11:59 PM

Answers

All replies

  • For dynamic loading you can load the contents of a file that exists in your app package, then eval() it.

    To get to files in your package, use Windows.ApplicationModel.Package.installedLocationAsStorageFolder, (see http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.package.installedlocationasstoragefolder.aspx). This gives you a StorageFolder object whose getFileAsync method (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefolder.getfileasync.aspx) gets you a StorageFile object.

    From there, see the "Reading a file" section of http://msdn.microsoft.com/en-us/library/windows/apps/hh464978.aspx for loading the file contents.

    Once you have that JavaScript in a string, you can then eval it.

    Note that while you can dynamically load files like this from your package contents, you cannot do the same from your application data folders as I wrote on this thread.

    .Kraig

    • Marked as answer by Map user Monday, January 9, 2012 6:13 PM
    Saturday, January 7, 2012 11:43 PM
  • Hi Map,

    Why don't you include it in your application?  Why don't you want to refer to it in the HTML?

    -Jeff


    Jeff Sanders (MSFT)
    Monday, January 9, 2012 2:01 PM
    Moderator
  • Thanks Kraig.

     

    Monday, January 9, 2012 6:13 PM
  • Kraig, I am still dreaming of ChromeExtension or Firefox GreaseMonkey extensibility for our Metro apps.

    So I would (with the users permissions) download JS code from the web or using the filepicker and insert the textcontent into a script tag dynamically during runtime. Would that work, or would the sandbox prevent me from doing that?

    Saturday, February 11, 2012 11:28 AM
  • Please visit thi link

    http://afarkas.github.com/webshim/demos/

    Monday, February 13, 2012 5:13 AM
  • I haven't tried dynamically adding a <script> tag to the DOM with a bunch of inline content, but that should work. You can also just eval a block of script which has the same effect. In these cases you as the developer (and the user for that matter) are conscious of what you're doing; the restrictions are primarily to prevent unknown attacks.

    Monday, February 13, 2012 5:09 PM