initialization-iframe.html
1.55 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Base source for injecting into an iframe for tests</title>
<script src="../../webcomponentsjs/webcomponents.js"></script>
<link rel='import' href='./initialization-cases.html'>
</head>
<body>
<script>
window.addEventListener("message", messageReceived, false);
window.addEventListener('WebComponentsReady', function() {
window.parent.postMessage({
'type': 'ready'
}, '*');
});
var appendBodyReceived = false;
function messageReceived(msg) {
if (!msg.data) {
console.error('got invalid msg?');
}
// the parent can (at any time) ask for our URL.
if (msg.data.type === 'urlQuery') {
msg.source.postMessage({
'type': 'urlQueryResponse',
'href': window.location.href,
'pathname': window.location.pathname,
'hash': window.location.hash,
'search': window.location.search
}, '*');
} else if (msg.data.type === 'appendBody') {
if (appendBodyReceived) {
throw new Error('should only receive at most one appendBody call');
}
var element = document.createElement(msg.data.tagName);
document.body.appendChild(element);
appendBodyReceived = true;
}
}
window.addEventListener('error', function(e) {
window.parent.postMessage({
'type': 'error',
'error': e.message
}, '*');
});
</script>
</body>
</html>