JavaScript javascript: window.close(); after X amount of seconds javascript window close event,javascript window close self,javascript window

I have a pop-up with an onclick action to close the box when the submit button is pressed:

<input type="submit" name="submit" value="submit" onclick="javascript: window.close();">

I'm trying to adapt it to wait 3 seconds after the submit button is clicked to close.

I read an article that suggested using this in the :

<script> 
setTimeout("window.close()",3000) 
</script>

but it closes the pop-up after 3 seconds regardless of whether or not the button is clicked.

How can I adapt it to work with the button?

Answer:1

Your first version works as written

<input type="submit" name="submit" value="submit" onclick="javascript: window.close();">

In that it will close the window when clicked.

Your second version works as written. But you seem a little confused so let me explain a little bit.

<script> 
 setTimeout("window.close()",3000) 
</script>

When this is inlined in your html file then it will execute when it is reached while the view is rendering. When executed, this code says to construct a new Function from the string argument, and then call it in 3000 milliseconds.

It is not tied to anything, it just runs when the rendering engine comes across it.

In order to tie this to something (and not just take the shortcut of placing it on the element as shown in the first version) you will need an event. In javascript, this event will be the click event. In order to access the click event, you need to place a clickeventhandler on the element you wish to handle an event for. And in order to have access to the element you must query the page for it when the DOM is loaded.

querySelectorAll documentation

<script>
window.onload = function(){ //this line of code will wait for the DOM to load
 //once loaded, it will execute the "anonymous" function which is the code here

 //now in here we can find the element
 //since there is no id or class, we can look for an input with name=submit
 var submitInput = document.querySelectorAll("input[name=submit]")[0];

 //once the element is found, we can attach the handler to it
 submitInput.onclick = function(){ //once again this "anonymous" function executes when the event fires
  //in here you can execute the timeout
  //lets use an anonymous function instead of constructing one from a string
  setTimeout(function(){
   window.close();
  },3000);
 };
};
</script>

After doing that, you can remove the onclick event handler from the element

<input type="submit" name="submit" value="submit" />

And there you have it, the best practice approach for your situation.

jQuery

If you are set on using jQuery to do this, you can use the shortcut for the document.ready functionality (as in wait for all the elements to be available) and then in the callback of that shortcut you would target the element (in jQuery you use css-ish selectors) and then tell jQuery to register for the click event, which involves another callback function.

It would look like this:

<script>
 $(function(){//document.ready shortcut
  $("input[name=submit]").click(function(){//target element and request click event
   setTimeout(function(){window.close();},3000);//timeout code to close window
  });
 });
</script>
Answer:2

Just try combining everything:

<input type="submit" name="submit" value="submit" onclick="setTimeout('window.close()',3000);">
Answer:3

I have a jsfiddle. obj = {}; obj[0] = 'hej'; obj[1] = 'hopp'; $.each( obj, function( key, value ) { console.log($.now()); }); $.now() is the same number in the console. How can I generate a ...

I have a jsfiddle. obj = {}; obj[0] = 'hej'; obj[1] = 'hopp'; $.each( obj, function( key, value ) { console.log($.now()); }); $.now() is the same number in the console. How can I generate a ...

  1. jquery unique array
  2. jquery unique id
  3. jquery uniquesort
  4. jquery unique values in array
  5. jquery unique array of objects
  6. jquery unique id generator
  7. jquery unique array of strings
  8. jquery unique not working
  9. jquery unique selector
  10. jquery unique validation
  11. jquery unique function
  12. jquery unique method
  13. jquery uniqueid is not a function
  14. jquery unique random number generator
  15. jquery unique select options
  16. jquery .uniqueid() example
  17. jquery unique id for element
  18. jquery unique example
  19. jquery unique json
  20. jquery unique list

I am trying to animate open (+) and close (x) buttons for main navigation. It's not working properly and I am getting this error. Uncaught TypeError: Cannot read property 'addEventListener' of null ...

I am trying to animate open (+) and close (x) buttons for main navigation. It's not working properly and I am getting this error. Uncaught TypeError: Cannot read property 'addEventListener' of null ...

  1. cannot read property of undefined
  2. cannot read property 'length' of undefined
  3. cannot read property 'setstate' of undefined
  4. cannot read property 'push' of undefined
  5. cannot read property 'map' of undefined
  6. cannot read property 'addeventlistener' of null
  7. cannot read property 'match' of undefined
  8. cannot read property 'then' of undefined
  9. cannot read property '0' of undefined
  10. cannot read property 'props' of undefined
  11. cannot read property 'nativeelement' of undefined
  12. cannot read property 'tolowercase' of undefined
  13. cannot read property 'state' of undefined
  14. cannot read property 'foreach' of undefined
  15. cannot read property 'split' of undefined
  16. cannot read property of null
  17. cannot read property 'replace' of undefined
  18. cannot read property 'summary' of undefined
  19. cannot read property 'createelement' of undefined
  20. cannot read property 'subscribe' of undefined

I am buried in a RegExp hell and can't find way out, please help me. I need RegExp that matches only numbers (at least 1 number) and one of this characters: <, >, = (exactly one of them one time). ...

I am buried in a RegExp hell and can't find way out, please help me. I need RegExp that matches only numbers (at least 1 number) and one of this characters: <, >, = (exactly one of them one time). ...

  1. regex numbers special characters
  2. regex letters numbers special characters
  3. regex numbers and special characters only
  4. regex no numbers or special characters
  5. regex remove numbers and special characters
  6. regex match numbers and special characters
  7. regex for numbers and special characters javascript
  8. regex exclude numbers and special characters
  9. regex for numbers and special characters in java
  10. c# regex numbers and special characters
  11. regex only numbers and special characters javascript
  12. regex remove numbers and special characters c#
  13. regex remove numbers and special characters javascript
  14. regex only numbers without special characters
  15. regex special numbers

I'm writing a simple text-base game in my free time. It's called TARDIS flight (Doctor Who!) and I'm working on the main menu. So I'm using a function, addMainMenuListeners, to add all the event ...

I'm writing a simple text-base game in my free time. It's called TARDIS flight (Doctor Who!) and I'm working on the main menu. So I'm using a function, addMainMenuListeners, to add all the event ...