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 Answers 2
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;
3 Comments
window can be overwritten (unfortunately), but it's a really bad idea to do so.window object.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...
rw,rh, andpopup. Other than that, you should rename the function to something other than "window."