Did you know you could use iFrames in WinRT apps developed using JavaScript? Is this suppose to be exciting? Why would you care? For the answers, follow along as I take a journey of discovery attempting to add live Twitter feeds to my app.

To set the stage for what I want to do, you must first understand what a Twitter Widget is. The folks at Twitter have made it real easy for web developers to create a custom, dynamic section of a web site to display tweets based on a user, a search, favorites, or a list. Once I know my tweet criteria, I can customize the appearance, dimensions, and other relevant options. When I am done with all my customizations, I can grab the code that will make the magic happen on my web site. Um… but I want this in my WinRT app. Will it work? Lets find out.

Now I will add this code to my project in Visual Studio 2012. I have just created this project using a blank template. In the default.html file, I will add the Twitter widget code I created above. Here is what my markup looks like:

Why did the script not execute? Because my default.html page is considered local in context to my application. The only way an external script would be allowed to execute is if it was executed in a web context. Well who decided those rules? Perhaps a better question for now is – How do I know if I am in local context or web context?

Any HTML file that is physically part of a project is considered as local in context. The converse to this is any HTML that resides externally or is remote to a project is considered to be in web context. How can I execute HTML remotely in my WinRT app?

Using iFrames

I can use an iframe in my HTML to point to an external web page. To demonstrate, I will remove the script I added to my default.html page (we will return to that code later), and replace the contents of the body tag with the following:

This will cause the iFrame to load the respective href value once either of the anchor tags are clicked.

Recommended: Create an HTML file in the root directory of your project namedmsapp-error.html. This file will be loaded automatically in the iframe when errors occur due source resources not loading or not found!

Now returning to my original objective, I would like to see the code I grabbed from Twitter work in my app still. I have an idea! I will take Twitter widget code and put in in a new HTML file in my project. I will name the file twitterframe.html, and create it in the root project directory. Here is the contents of that file:

Will I get my desired output? Can I trick the iframe to run a page in local context but treat it like it is in web context? Not the way I am doing it. When I run my application, I get the same error I received earlier. However, I was on the right track of thinking. By using a special moniker preceding the URL, I can ask for the local page to be executed in a web context. Here is the syntax for that: