Change the Javascript Window.Open to open an Iframe

0

I have this javascript, and I want to do that instead of opening a new window, it opens an iframe.

 window.self.name = "emanager";
 function MostraBarra(){
 if (document.incluir.balancete_arquivo.value != ""){
 window.open('<%=BARRASTATUS%>','upload','width=400,height=150');
 }
 return true;
 }
    
asked by anonymous 17.07.2018 / 00:39

3 answers

0

You just need to do one

if (document.incluir.balancete_arquivo.value != ""){
    var frame = document.getElementById('iframe') //Id do seu iframe aqui
    frame.src = '<%=BARRASTATUS%>'; // Considerando que BARRASTATUS 
                                   //  tenha o url que vai ser passado pro frame
}

Having this, the iframe will load the other screen; Now you need to set the size, visibility, etc ...

    
17.07.2018 / 01:26
1

An iframe is not something that "opens" in the same way that a window opens. It is an HTML tag that you insert into the page and load another page.

What you can do is insert the tag where you want it on the page without specifying a URL and hidden with display: none; :

<iframe style="display: none;"></iframe>

In the function you change the visibility of iframe and set a src that will be the page to be loaded:

function MostraBarra(){
   if (document.incluir.balancete_arquivo.value != ""){
//      window.open('<%=BARRASTATUS%>','upload','width=400,height=150');
      var iframe = document.querySelector("iframe"); // seleciona o iframe pela tag
      iframe.style.display = "block"; // altera o display tornado-o visível
      iframe.src = "<%=BARRASTATUS%>"; // carrega uma página no iframe
   }
   return true;
}

Then you can set styles of iframe via CSS any way you want:

iframe{
   width: 100%;
   height: 400px;
   border: none;
}

Other properties of iframe you can check in MDN documentation .

    
17.07.2018 / 01:27
0

Javascript

function prepareFrame() {
    if (document.incluir.balancete_arquivo.value != ""){
       var ifrm = document.createElement("iframe");
       ifrm.setAttribute("src", "<%=BARRASTATUS%>");
       ifrm.style.width = "400px";
       ifrm.style.height = "150px";
       document.body.appendChild(ifrm);
   }
}

HTML

<button type="button" onclick="prepareFrame()">Criar iframe</button>

To create the iframe in the exact location:

function prepareFrame() {
    var ifrm = document.createElement("iframe"); 
    ifrm.setAttribute("src", "radioalfa2233.htm");
    ifrm.style.width = "400px";
    ifrm.style.height = "150px";
    //o iframe será criado antes da div de id = divqq
    var divReferencia = document.getElementById("divqq");
    // adiciona o novo elemento criado e seu conteúdo ao DOM
    document.body.insertBefore(ifrm, divReferencia);
}

and in the HTML insert this% div_with% in the exact place where the iframe should be created

If you want to hide the button after clicking

add this line inside the <div id="divqq"></div> in the script

if

and the id on the button document.getElementById('bt').style.display = 'none';

  • The <button id="bt".... function will create an HTML element to be inserted into an HTML document
  • Apparently, the createElement() function has no effect, we need to apply the createElement() method so that the element is effectively inserted into the HTML document and visible to the user.
17.07.2018 / 01:40