2

anytime I'm trying to reference my global variable i get an Uncaught TypeError: undefined is not a function error and nothing happens. I'm probably missing something obvious since I'm new to javascript. I've simplified the code below so that you can give it a look. All help is much appreciated.

var popup;

function first(){
    popup = window.open(...); //opens a popup
    popup.moveTo(10,10) //moves the window
    second();
}

function second(){
    .
    .
    .
    //does an XMLHttpRequest and when it's done (simplified) it tries to close the window
    xmlHttp.onreadystatechange = function(){
        popup.close();
    }
}

Whenever popup.close() is fired I get the error above. Am I missing something really obvious here?

For future problems as this, here is a solution:

instead of popup.close();

var win = window.open("","popup");//open a new window with the same name as before
win.close();

By doing so it will reference the same window object and will be able to close it.

7
  • You dont have function close in the pop object Commented Jul 31, 2014 at 8:15
  • 2
    You're calling second before you're calling first. Commented Jul 31, 2014 at 8:16
  • 1
    I suspect the same as @BenjaminGruenbaum. But you could easily avoid such mistakes by defining popup inside first and passing it to second as argument. Commented Jul 31, 2014 at 8:19
  • @user2071225 I see what you're thinking but first(); has to run when I want it to and not on startup.. Shouldn't it be a global reference? Commented Jul 31, 2014 at 8:19
  • @user2970038 You need to post how you call these functions if you want to get this solved. Can you create a JSFiddle that demonstrates the problem? Commented Jul 31, 2014 at 8:24

2 Answers 2

2

From your error, there is two possibility

  1. pop is not accessible
  2. close does not exist in pop object

I tried both in console

When object pop does not exist

  pop.close()

Error you will get

  ReferenceError: pop is not defined

When no function close in the object

 var pop = {}
 pop.close()

Error you will get

 TypeError: undefined is not a function

From your error, there is no function name close in the pop object.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this one..

var popup;

function first(){
    popup = window.open(...); //opens a popup
    popup.moveTo(10,10) //moves the window
    second(popup);
}

function second(popup_){
    .
    .
    .
    //does an XMLHttpRequest and when it's done (simplified) it tries to close the window
    xmlHttp.onreadystatechange = function(){
        popup_.close();
    }
}

1 Comment

It's giving me the same error which is weird since popup.moveTo works :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.