File: popup-iframes.html

package info (click to toggle)
jquery-mobile 1.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,068 kB
  • ctags: 243
  • sloc: php: 113; makefile: 54
file content (272 lines) | stat: -rw-r--r-- 11,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>jQuery Mobile Docs - Popup iframes</title>
	<link rel="stylesheet"  href="../../../css/themes/default/jquery.mobile-1.2.0.css" />
	<link rel="stylesheet" href="../../_assets/css/jqm-docs.css"/>

	<script src="../../../js/jquery.js"></script>
	<script src="../../../docs/_assets/js/jqm-docs.js"></script>
	<script src="../../../js/jquery.mobile-1.2.0.js"></script>

	<script src="popup-examples.js"></script>
	<link rel="stylesheet" href="popup-examples.css" />

</head>
<body>

	<div data-role="page" class="type-interior">

	<div data-role="header" data-theme="f">
		<h1>Popups with iframes</h1>
		<a href="../../../" data-icon="home" data-iconpos="notext" data-direction="reverse">Home</a>
		<a href="../../nav.html" data-icon="search" data-iconpos="notext" data-rel="dialog" data-transition="fade">Search</a>
	</div><!-- /header -->

	<div data-role="content" class="ui-body">
		<div class="content-primary">
			
			<a href="index.html" data-role="button" data-transition="fade" data-icon="arrow-l" data-inline="true" data-mini="true">Back to Popups</a>
			<h2>Working with iframes in popups</h2>
			
			<p>You may need to embed an iframe into a popup to use a 3rd party widget. Here, we'll walk through a few real-world examples of working with iframes: videos and maps.</p>

			<h2>Video example</h2>
			
			<p>Here is an example of a 3rd party video player embedded in a popup:</p>
				<a href="#popupVideo" data-rel="popup" data-position-to="window" data-role="button" data-theme="b" data-inline="true">Launch video player</a>			

				<div data-role="popup" id="popupVideo" data-overlay-theme="a" data-theme="d" data-tolerance="15,15" class="ui-content">
					<iframe src="http://player.vimeo.com/video/41135183?portrait=0" width="497" height="298" seamless></iframe>
				</div>
				
			<p>The markup is an iframe inside a popup container. The popup will have a 15 pixels padding because of class <code>ui-content</code> and a one pixel border because the framework will add class <code>ui-body-d</code> to the popup.</p>
			
<pre><code>
&lt;div data-role="popup" id="popupVideo" data-overlay-theme="a" data-theme="d" data-tolerance="15,15" class="ui-content"&gt;

    &lt;iframe src="http://player.vimeo.com/video/41135183" width="497" height="298" seamless&gt;&lt;/iframe&gt;
	 
&lt;/div&gt;
</code></pre>

			<p>When using an iframe inside a popup it is important to initially <strong>set the width and height attributes to 0</strong>. This prevents the page to breaking on platforms like Android 2.3. Note that you have to set the attributes, because setting the width and height with CSS is not sufficient. You can leave the actual width and height in the markup for browsers that have JavaScript disabled and use <code>attr()</code> to set the zero values upon the <code>pageinit</code> event.</p>

			<p>Next, bind to the <code>popupbeforeposition</code> event to set the desired size of the iframe when the popup is shown or when the window is resized (e.g. orientation change). For the iframe examples on this page a custom function <code>scale()</code> is used to scale down the iframe to fit smaller screens. Expand the section below to view the code of this function.</p>

			<div data-role="collapsible" data-content-theme="d">
				<h3><code>scale()</code></h3>
				<p>The window width and height are decreased by 30 to take the tolerance of 15 pixels at each side into account.</p>
<pre><code>
function scale( width, height, padding, border ) {
    var scrWidth = $( window ).width() - 30,
        scrHeight = $( window ).height() - 30,
        ifrPadding = 2 * padding,
        ifrBorder = 2 * border,
        ifrWidth = width + ifrPadding + ifrBorder,
        ifrHeight = height + ifrPadding + ifrBorder,
        h, w;

    if ( ifrWidth < scrWidth && ifrHeight < scrHeight ) {
        w = ifrWidth;
        h = ifrHeight;
    } else if ( ( ifrWidth / scrWidth ) > ( ifrHeight / scrHeight ) ) {
        w = scrWidth;
        h = ( scrWidth / ifrWidth ) * ifrHeight;
    } else {
        h = scrHeight;
        w = ( scrHeight / ifrHeight ) * ifrWidth;
    }

    return {
        'width': w - ( ifrPadding + ifrBorder ),
        'height': h - ( ifrPadding + ifrBorder )
    };
};
</code></pre>
				<p><strong>Note:</strong> This function is not part of the framework. Copy the code into your script to use it.</p>
			</div>		
			
			<p>When the popup is closed the width and height should be set back to 0. You can do this by binding to the <code>popupafterclose</code> event.</p>
			
			<p>This is the complete script and the link to open the video popup:</p>
			
<pre><code>
$( document ).on( "pageinit", function() {
    $( "#popupVideo iframe" )
        .attr( "width", 0 )
        .attr( "height", 0 );
		  
    $( "#popupVideo" ).on({
        popupbeforeposition: function() {
            var size = scale( 497, 298, 15, 1 ),
                w = size.width,
                h = size.height;

            $( "#popupVideo iframe" )
                .attr( "width", w )
                .attr( "height", h );
        },
        popupafterclose: function() {
            $( "#popupVideo iframe" )
                .attr( "width", 0 )
                .attr( "height", 0 );    
        }
    });
});
</code></pre>
			
            <p>Note that the video will still be playing in the iframe when the popup is closed. If available you can use the 3rd party API to stop the video on the <code>popupafterclose</code> event. Another way is to create the iframe when the popup is opened and destroy it when the popup is closed, but this would drop support for browsers that have JavaScript disabled.</p>
		
			<h2>Map example</h2>
			<p>In the second example, an iframe is used to display <strong>Google Maps API</strong>. Using an iframe prevents issues with the controls of the map.</p>
			
			<a href="#popupMap" data-rel="popup" data-position-to="window" data-role="button" data-theme="b" data-inline="true">Open Map</a>

			<div data-role="popup" id="popupMap" data-overlay-theme="a" data-theme="a" data-corners="false" data-tolerance="15,15">
				<a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
				<iframe src="map.html" width="480" height="320" seamless></iframe>
			</div>
			
			<p>This is the markup of the popup including a right close button:</p>
<pre><code>
&lt;div data-role="popup" id="popupMap" data-overlay-theme="a" data-theme="a" data-corners="false" data-tolerance="15,15"&gt;

    &lt;a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close&lt;/a&gt;
	 
    &lt;iframe src="map.html" width="480" height="320" seamless&gt;&lt;/iframe&gt;
	 
&lt;/div&gt;
</code></pre>

			<p>Expand the section below to view the source of the iframe.</p>

			<div data-role="collapsible" data-content-theme="d">
				<h3><code>map.html</code></h3>
<pre><code>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Map&lt;/title&gt;
    &lt;script&gt;
        function initialize() {
            var myLatlng = new google.maps.LatLng( 51.520838, -0.140261 );
            var myOptions = {
                zoom: 15,
                center: myLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            var map = new google.maps.Map( document.getElementById( "map_canvas" ), myOptions );
        }
    &lt;/script&gt;
    &lt;script src="http://maps.google.com/maps/api/js?sensor=false"&gt;&lt;/script&gt;
    &lt;style&gt;
        html {
            height: 100%;
            overflow: hidden;
        }
        body {
            margin: 0;
            padding: 0;
            height: 100%;
        }
        #map_canvas { 
            height: 100%;
        }    
    &lt;/style&gt;
&lt;/head&gt;
&lt;body onload="initialize()"&gt;

    &lt;div id="map_canvas"&gt;&lt;/div&gt;
    
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
			</div>
				
			<p>Setting the size of the iframe is done exactly the same as for the video example, with one exception. You should also set the width and height of the div that contains the map to prevent the page to break on platforms like Android 2.3. In this example the ID of this div is <code>#map_canvas</code>.</p>
			
			<p>This is the complete script and the link to open the map popup:</p>
			
<pre><code>
$( document ).on( "pageinit", function() {
    $( "#popupMap iframe" )
        .attr( "width", 0 )
        .attr( "height", 0 );
		  
    $( "#popupMap iframe" ).contents().find( "#map_canvas" )
        .css( { "width" : 0, "height" : 0 } );
	 	     
    $( "#popupMap" ).on({
        popupbeforeposition: function() {
            var size = scale( 480, 320, 0, 1 ),
                w = size.width,
                h = size.height;

            $( "#popupMap iframe" )
                .attr( "width", w )
                .attr( "height", h );
					 
            $( "#popupMap iframe" ).contents().find( "#map_canvas" )
                .css( { "width": w, "height" : h } );
        },
        popupafterclose: function() {
            $( "#popupMap iframe" )
                .attr( "width", 0 )
                .attr( "height", 0 );
					 
            $( "#popupMap iframe" ).contents().find( "#map_canvas" )
                .css( { "width": 0, "height" : 0 } );
        }
    });
});
</code></pre>			
			
			
			
		</div><!--/content-primary -->

				<div class="content-secondary">

					<div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">

							<h3>More in this section</h3>

							<ul data-role="listview" data-theme="c" data-dividertheme="d">

								<li data-role="list-divider">Pages &amp; Dialogs</li>
								<li><a href="../page-anatomy.html">Anatomy of a page</a></li>
								<li><a href="../page-template.html" data-ajax="false">Single page template</a></li>
								<li><a href="../multipage-template.html" data-ajax="false">Multi-page template</a></li>
								<li><a href="../page-titles.html">Page titles</a></li>
								<li><a href="../page-links.html">Linking pages</a></li>
								<li><a href="../page-transitions.html" data-ajax="false">Page transitions</a></li>
								<li><a href="../loader.html">Page loading widget</a></li>
								<li><a href="../dialog/index.html">Dialogs</a></li>
								<li data-theme="a"><a href="index.html">Popups</a></li>
								<li><a href="../page-cache.html">Prefetching &amp; caching pages</a></li>
								<li><a href="../page-navmodel.html">Ajax, hashes &amp; history</a></li>
								<li><a href="../page-dynamic.html">Dynamically Injecting Pages</a></li>
								<li><a href="../page-scripting.html">Scripting pages</a></li>
								<li><a href="../phonegap.html">PhoneGap apps</a></li>
								<li><a href="../touchoverflow.html">touchOverflow feature</a></li>
								<li><a href="../pages-themes.html">Theming pages</a></li>

							</ul>
					</div>
				</div>

</div><!-- /content -->

<div data-role="footer" class="footer-docs" data-theme="c">
		<p class="jqm-version"></p>
		<p>&copy; 2012 jQuery Foundation and other contributors</p>
</div>

</div><!-- /page -->

</body>
</html>