locked
using jQuery to load in dynamic content in Metro style app

    Question

  • What I'm trying to accomplish:

    'Single-page' app that loads in local HTML files into default.html.  These HTML files may or may not contain scripts.

    To accomplish this, I had planned to use jQuery.load().  However, I run into security warnings and my scripts get stripped out.  Wrapping my jQuery.load call inside of MSApp.execUnsafeLocalFunction doesn't do the trick.  Appending the page path passed to load with "ms-wwa:///" doesn't do the trick.  It seems that the only fix is going through my jQuery file and wrapping every single .innerHTML call with execUnsafeLocalFunction.

    There has got to be a simple workaround for this... The content loaded in is local and trusted, not coming in from the web.  Is there a WinJS substitute for jQuery's load function?  Or a way to get Windows 8 and jQuery to fully cooperate? 

    Wednesday, August 15, 2012 8:48 AM

Answers

  • Hi Nimbus,

    Wow...  You do not have to be abusive in your forum posts.  You may have noticed there are several people assisting you and sometimes it takes a little time to get back to people.

    You can work around security restrictions so you can make an intelligent decision yourself if you wish to override something.  That is why we provide the option of using execUnsafeLocalFunction.  The default is to protect you however.

    jQuery is used in apps already quite a bit.  If you are have a specific error it may have been already discussed in the forums so you can search for that particular error or post a simple repro of the issue you are having.  Generic questions usually only get generic answers due to their nature.

    You do not have to use WinJS, but WinJS will give you the advantage of easily creating the familiar Windows Store app UI and has nice built in controls that have animation.

    -Jeff


    Jeff Sanders (MSFT)

    Monday, August 20, 2012 1:12 PM
    Moderator

All replies

  • Hi Nimbus,

    You don't need to use jQuery.  WinJS fully supports loading HTML pages.

    Start here:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh452768.aspx

    http://code.msdn.microsoft.com/windowsapps/Page-Controls-sample-568b10b4

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, August 15, 2012 2:55 PM
    Moderator
  • Well that's cool about the page controls and all, but is there a way I can just do this all myself?  It just feels too rigid to me and when I come across problems, scouring the internet for answers on WinJS is very difficult.  I'd rather create my own page control system since it's not difficult (or shouldn't be at least!) and it would provide me with full control and knowledge of what's going on behind the scenes.

    Here's my issue with the page control examples.  It is still unclear to me how I would load in page fragments.  This is obviously a no-brainer with jQuery.  Using WinJS navigation, I can load in full pages.  But do I really have to specify the same header over and over and over again on each page that contains it?

    This really feels like things have made things unnecessarily complicated for HTML/JS developers.  Especially with the lack of support for jQuery. 

    Wednesday, August 15, 2012 5:16 PM
  • Take a look at the WinJS.UI.Fragments functionality, it is the base for pages and provides the ability to load a document in as a child of an element.

    http://msdn.microsoft.com/en-us/library/windows/apps/br229781.aspx

    -Sam

    Wednesday, August 15, 2012 7:57 PM
  • But why not simply support jQuery?  Is there a way around the security policy (besides surrounding every line of .innerHTML with execUnsafeLocalFunction)?

    I would much rather keep my code generic and use jQuery.  I'm thinking about this in terms of porting my apps to other platforms. 

    Also, that WinJS.UI.Fragments documentation sucks.  Can someone provide an actual example of it replacing a page fragment with code from a file?

    • Edited by Nimbus510 Wednesday, August 15, 2012 11:07 PM
    Wednesday, August 15, 2012 9:27 PM
  • Samples are your friend: http://code.msdn.microsoft.com/windowsapps/Fragments-91f66b07


    Jeff Sanders (MSFT)

    Friday, August 17, 2012 1:29 PM
    Moderator
  • I'm grateful that you provided the link to that sample, but don't just ignore the other questions!  

    Okay, let me lay out some questions that I'm sure you will continue to ignore:

    -Why implement the security policy if it can easily be worked around?  Can't you trust that devs won't be dumb with remote data sources and remove the annoying task of surrounding calls in execUnsafeLocalFunction?

    -Can the security settings be configured within an app so that plugins like jQuery work without needing to be heavily modified?

    -Why try to reinvent jQuery functionality when it already exists and is so widely used and familiar to devs?

    The big thing here: I want to be able to port my app!  Which is a pain in the butt if I build it tightly coupled with MS-only libraries!  Also, I want to be able to apply my existing knowledge, not spend hours reading about some fancy library I will never ever use anywhere else.

    Friday, August 17, 2012 9:17 PM
  • Hi Nimbus,

    Wow...  You do not have to be abusive in your forum posts.  You may have noticed there are several people assisting you and sometimes it takes a little time to get back to people.

    You can work around security restrictions so you can make an intelligent decision yourself if you wish to override something.  That is why we provide the option of using execUnsafeLocalFunction.  The default is to protect you however.

    jQuery is used in apps already quite a bit.  If you are have a specific error it may have been already discussed in the forums so you can search for that particular error or post a simple repro of the issue you are having.  Generic questions usually only get generic answers due to their nature.

    You do not have to use WinJS, but WinJS will give you the advantage of easily creating the familiar Windows Store app UI and has nice built in controls that have animation.

    -Jeff


    Jeff Sanders (MSFT)

    Monday, August 20, 2012 1:12 PM
    Moderator