How to close a page automatically

…this post was published on 2012 January 5.

Starting from a post on Sharepoint Forum, a friend of mine just asked how it is possible to open a page, and close it automatically.

Well, I created an example:

​How can you open a  page , and open it in different window? just call it using attribute target=”_blank” in <a href > tag, like:

<a href=”your_link” target=”_blank”>Text to your link</a>

In that new page, i inserted next  script code, just after

<asp:Content ContentPlaceHolderId=”PlaceHolderMain” runat=”server”>

<script type=”text/javascript”>
  var t_initial=15;
  var t_left=t_initial;
  setTimeout(‘after_time()’,15000);
  setInterval(‘in_time()’,1000);

  function after_time(){
   alert(‘Booommmmm! !!! ‘+’\nTIME OUT’);
   window.close();
  }
   
  function in_time( ){
   t_left = t_left-1;
   document.getElementById(‘time__left’).innerHTML = t_left ;

   }
 </script>

Let  me explain:

First I defined 2 variables, t_initial equal to 15 seconds, and t_left, which will decrement by 1 each second.

Next I called 2 functions:

SetTimeout() which will start after 15 seconds, and will run window.close()  to close that window.

and the second one:

setInterval() which run a function, in my case named in_time() at each 1000 miliseconds

Last but not least, I wrote  some words in the body of the page, in the webPart, using this code:

<br />
     THIS PAGE WILL CLOSE IN ..
     <span style=”font-size:medium;color:blue” id=”time__left”></span> 
     &nbsp;&nbsp; SECONDS<br />
     JUST WAIT and ….
<br />

As you can see there you can find the tag <span> identified by ID=”time__left” and which is called by function setInterval()  to show remaining seconds.

You can read more about: target; setTimeout(); setInterval()

This entry was posted in Coding, Javascript. Bookmark the permalink.