How to Use IFrames in WinRT Apps

SNAGHTML25abaef5Did 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.

I will choose to create a Twitter Search Widget.  Here is a screen capture of my criteria:

SNAGHTML25bbc55e

Once I click the [Finish & Grab Code] button, I see the following:

SNAGHTML25be0be3

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:

 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>HowTo_IFrames</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
    <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>

    <!-- HowTo_IFrames references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
</head>
<body>
    <p>Content goes here</p>
    <script charset="utf-8" 
             src="https://widgets.twimg.com/j/2/widget.js"></script>
    <script>
            new TWTR.Widget({
            version: 2,
            type: 'search',
            search: '#win8appdev',
            interval: 30000,
            title: 'Windows 8 Developers',
            subject: '#win8appdev',
            width: 250,
            height: 300,
            theme: {
                shell: {
                    background: '#8ec1da',
                    color: '#ffffff'
                },
                tweets: {
                    background: '#ffffff',
                    color: '#444444',
                    links: '#1985b5'
                }
            },
            features: {
                scrollbar: false,
                loop: true,
                live: true,
                behavior: 'default'
            }
        }).render().start();
    </script>
</body>
</html>

Will it be that simple?  Unfortunately no.  When I attempt to run the application, I get the following error:

SNAGHTML25cae34d

It may not be obvious by the error message what the real issue is.  The reason why ‘TWTR’ is undefined is due to the following script not being executed:

 <script charset="utf-8"             
        src="https://widgets.twimg.com/j/2/widget.js"></script>

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:

 <h1>iFrame Demo</h1>
<iframe src="https://www.palermo4.com"
        width="900"
        height="600">
</iframe>

When I run my application now, this is what I see:

image

If this is all I do, I essentially have a “browser” to my site within my app.  I could also change the source of the iframe from anchor tags as seen with the following revisions:

 <h1>iFrame Demo</h1>
<div>
    <a href="https://palermo4.com" target="framed">Palermo4</a> 
    <a href="https://codefoster.com" target="framed">Codefoster</a>
</div>
<iframe name="framed" width="900" height="600">
</iframe>

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 named
msapp-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:

 <!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script charset="utf-8" 
                 src="https://widgets.twimg.com/j/2/widget.js">
         </script>
    <script> 
        new TWTR.Widget({
            version: 2,
            type: 'search',
            search: '#win8appdev',
            interval: 30000,
            title: 'Windows 8 Developers',
            subject: '#win8appdev',
            width: 250,
            height: 300,
            theme: {
                shell: {
                    background: '#8ec1da',
                    color: '#ffffff'
                },
                tweets: {
                    background: '#ffffff',
                    color: '#444444',
                    links: '#1985b5'
                }
            },
            features: {
                scrollbar: false,
                loop: true,
                live: true,
                behavior: 'default'
            }
        }).render().start();
    </script>
    </body>
</html>

Now I will return to default.html, and modify the contents within the body tags as follows:

 <h1>iFrame Demo</h1>
<iframe src="/twitterframe.html" width="300" height="400">
</iframe>

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:

 <iframe src="ms-appx-web:/// twitterframe.html" 
        width="300" height="400">
</iframe>

By adding ms-appx-web:/// before my local page name, I am informing my application to run it in a web context. This crucial step gives me exactly what I want, as seen here:

image

Hooray!  I got my Twitter search widget to work in my WinRT app!  Think of the possibilities with any other social meshing sites or mapping tools!

By the way, you can govern what is allowed to happen in the iframe by setting the sandbox attribute.  IntelliSense reveals self-describing features:

SNAGHTML261ce5af

I hope you enjoy developing Windows 8 applications with JavaScript and HTML5!  For more resources, be sure to sign up for Generation App! Your idea. Your app. 30 days.

Cheers!