0

I am trying to make a button that will popup a new window using a javascript button pressed by the user. The button shows up on the site, but nothing happens when I press it. I've tried it in Chrome and Firefox. I can't figure out if it's browser settings or code error.

<button onclick="window()">Reminisce</button>

<script type="text/javascript">
function window() { 
    var w = window.innerWidth;
    var h = window.innerHeight;
    var rw = Math.random()*w)+1);
    var rh = Math.random()*h)+1);

    var popup = window.open('http://www.roberthayeskee.com/bush2.html,'_blank','width=rw,height=rh');
}
</script>

Thanks!

2
  • Is your popup blocker activated? Commented Jun 20, 2016 at 22:38
  • 3
    You have syntax errors on the lines declaring rw, rh, and popup. Other than that, you should rename the function to something other than "window." Commented Jun 20, 2016 at 22:42

2 Answers 2

3

window is a global object representing the current browser window and cannot be overwritten with your custom function.

When you press the button, an error message similar to the following should appear in the browser console, since window is an object, not a callable function:

Uncaught TypeError: window is not a function

You should rename your function, e.g. to openWindow.


Also, syntax errors as described in the comments above need to be resolved before the function can possibly work.

Edit: As described in the comments, it seems like you can overwrite the window object. That does still not mean you should, and it would also break the lines where you read the inner height/width:

var w = window.innerWidth;
Sign up to request clarification or add additional context in comments.

3 Comments

window can be overwritten (unfortunately), but it's a really bad idea to do so.
@RickHitchcock Interesting, just tried it in Chrome's console and it did not work. Always returned the usual window object.
I see... did not know that. Thanks!
2

In addition to TimoSta's answer, your code is full of syntax errors and typos. You probably want something along the lines of:

https://jsfiddle.net/hsfcc8sp/4/

function openWindow() { 
  var w = window.innerWidth;
  var h = window.innerHeight;
  var rw = Math.random()*w+1;
  var rh = Math.random()*h+1;

  var popup = window.open('http://www.roberthayeskee.com/bush2.html','_blank','width=' + rw,'height=' + rh);
}

But I had to change a lot of code...

Comments

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.