I inherited some JS code that I was asked to modify to invoke a webpage and insert it into an iframe. I asked this question before and was given a snippet of code that, by itself, does nothing. I inserted it into the JS code but what I am missing is how to get it to execute. I am learning JS so I am sure the answer may be fairly simple. The code I was given is:
JavaScript
x
24
24
1
document.addEventListener('keydown',function(e){
2
3
//SHIFT + something
4
if(e.altKey){
5
switch(e.code){
6
7
case 'KeyA': <----- this works fine and invokes the appropriate website
8
window.location = "http://www.website1.com/graphics/webpage4.html";
9
break;
10
11
case 'KeyZ': <----- this section does nothing that I can see with this code
12
function prepareFrame() {
13
var ifrm = document.createElement("iframe");
14
ifrm.setAttribute("src", "http://www.website1/graphics/webpage4.html");
15
ifrm.style.width = "640px";
16
ifrm.style.height = "480px";
17
document.body.appendChild(ifrm);
18
}
19
20
break;
21
22
23
}
24
Any help in getting the code to execute will be greatly appreciated
Advertisement
Answer
It does nothing because it only declares the prepareFrame
function and doesn’t call it.
JavaScript
1
8
1
case 'KeyZ':
2
function prepareFrame() {
3
// ...
4
}
5
6
prepareFrame()
7
break
8