Biomehanika
Biomehanika

Reputation: 1540

Javascript function only running on the first call

I have a function opening a new window, attached to a button:

<button onclick="newWin()">New Window</button>

This works fine but just the first time it is clicked. Following times, even when the opened window has been closed, I have this error:

TypeError: newWin is not a function

This is the JS function:

function newWin() {
       newWindow= window.open('pan/newWin.html', 'Nueva ventana', 'toolbar=yes,location=no,resizable=no,width=600,height=820');}

Upvotes: 1

Views: 162

Answers (2)

Grundy
Grundy

Reputation: 13382

You have two mistake:

  1. try save opened window in variable with same name as function
  2. not use var keyword.

In your case you just redefine global property newWin from function to object.

for solving you can rename it, or just use var keyword: var newWin = window.open(...)

Upvotes: 1

Dnyanesh
Dnyanesh

Reputation: 2343

Your are assigning some object to function variable.

First time on page load newWin() will be loaded as function. After execution of function you are assigning some value to it .

Hence it is resulting in to error.

Upvotes: 1

Related Questions