Skip to content
Advertisement

document.createElement(“iframe”) not working

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:

document.addEventListener('keydown',function(e){

 //SHIFT + something
  if(e.altKey){
  switch(e.code){

  case 'KeyA':        <----- this works fine and invokes the appropriate website
    window.location = "http://www.website1.com/graphics/webpage4.html";
    break;

  case 'KeyZ':        <----- this section does nothing that I can see with this code 
    function prepareFrame() {
    var ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", "http://www.website1/graphics/webpage4.html");
    ifrm.style.width = "640px";
    ifrm.style.height = "480px";
    document.body.appendChild(ifrm);
    }

    break;
    
    
}

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.

case 'KeyZ':
  function prepareFrame() {
    // ...
  }

  prepareFrame()
  break
Advertisement