Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

459
Visualizações
Passing a variable in JavaScript to open.window

I have variable which is i think global ,,so all my child functions must be able to get that variable,But i am getting a reference error,Variable not declared Here is below code.Please help if i am doing any wrong thing.Thanku

<!DOCTYPE html>
     <html>
        <head>
         <script>
            var Test1Object = 'Testing'; // This is my variable
         </script>
        <script src = 'ch.js'>
        </script>
        </head>
      <body>
        <button onclick="openwindow()">Create window</button>
      </body>
    </html>

My Ch.js

(function(){

    alert(Test1Object)  // Here i am getting this object
   this.openwindow = function() {

    w =window.open("untitled.html",'TheNewpop','height=315,width=625');  
    w.document.write(
    "<body>"+

    "<\/body>" + 
    "<script src = \"windowpo.js\"><\/script>" // THis is where i reference my windowpo.js

    )
    w.document.close();
    w.focus();
}
 })()

My windowpo.js

(function(){
alert(Test1Object)  // Here  there is not Test1Object (Reference error)
})();

My issue is that in my windowp.js how can i get my Test1Object Variable...

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Easy doing by just acessing your refrence inside the window by using window.opener like in this runnable demo plnkr. Inside your window application you can access it via window.opener.Test1Object where window.opener holds a reference of the JavaScript instance where it was opened. In that way you can access all the stuff you configured in your main application:

  • Source: window.opener MDN reference

View

<!DOCTYPE html>
<html ng-app="myApp">

<head lang="en">
  <meta charset="utf-8" />
  <title>Custom Plunker</title>
 <script type="text/javascript"> 
    var Test1Object = 'Testing';
 </script>
 <script src="main.js"></script>
</head> 
<body>
  <a onclick="openwindow()">Open Window</a>
</body>
</html>

main.js

this.openwindow = function() {
  w = window.open(location.href+"untitled.html",'TheNewpop','height=315,width=625');  
  w.document.close(); 
  w.focus();
}

unitiled.html

Some Test
<script src="windowpo.js"></script>

windowpo.js

alert(window.opener.Test1Object);
about 4 years ago · Santiago Trujillo Relatório

0

As most other answers don't seem to even read the question properly, I'll add my 2 cents as an answer, too:

The main problem in your code, is that you have to JS execution contexts here: One for the original page (the HTML code you show) and one for the popup you open in Ch.js. In general both do not share any data, variables or whatever.

You have, however, a window object reference in your variable w after calling window.open(). You use this already to inject HTML code to the popup.

If you now want to have JS variables available in the popup JS context, you can either inject additional <script> tags into the popups HTML code and set the variables there (bad choice, imho) or use postMessage() to send data across. I give some sample code for the postMessage() variant below:

Ch.js

this.openwindow = function() {

  w = window.open("untitled.html",'TheNewpop','height=315,width=625');  
  w.document.write(
    "<body>"+
    "<\/body>" + 
    "<script src = \"windowpo.js\"><\/script>" // THis is where i reference my windowpo.js
    );
  w.document.close();
  w.focus();

  // wait for pupup to be ready
  window.addEventListener( 'message', function( e ){
    // send the variable
    if( e.data == 'inited' ) {
      w.postMessage( Test1Object, '*' );
    }
  })
}

windowpo.js

// wait for messages from opener
window.addEventListener( 'message', function( e ) {
  alert( e.data );
});

// tell the opener we are waiting
window.opener.postMessage( 'inited', '*' );

For some more information see the respective MDN article on Window.postMessage().

about 4 years ago · Santiago Trujillo Relatório

0

You need to declare the variable before you include in any file. Simply create a script tag above the included files define it there.

<script type='text/javascript' > 
  var Test1Object = 'Testing';
</script>
<script type='text/javascript' src='js/Ch.js'></script> 
<script type='text/javascript' src='js/windowpo.js'></script>

this way you should be able to use withing all files

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda