I need to open a pop up window that closes automatically after x seconds. This code works fine outside Sharepoint:

 function openWin1() {
var myWindow1 = window.open("http://url here", "myWindow1", "width=500,height=400");

setTimeout(function(){myWindow1.close()}, 6000);

<button onclick="openWin1()"> Open "myWindow1"</button>

But when I add it to a SP Online publishing page, the "parent" page refreshes in the background - the pop up doesn't close.


SharePoint pages are forms and the default button is running your JavaScript and then submitting the form. Set the button type to "button" to override this default behavior.

<button type="button" onclick="openWin1()"> Open "myWindow1"</button>

expanding on mike, yes you need "type" also in the JS code just use # to refer to the current site

       function openWin1() {
          var myWindow1 = window.open("#", "myWindow1", "width=500,height=400");

          setTimeout(function(){myWindow1.close()}, 6000);

<input type="button" value="click here to open page" onclick="openWin1()"/>

<button type="button" onclick="openWin1()"> click here to open page</button>

both button types work fine.

  • thanks, but the site being opened in the pop up isn't the current site. – matt Mar 24 '18 at 2:16
  • just change the '#' , the code above opens up the same site and page that you put the code in. If you want a specific site than just update the '#' – Ali Jafer Mar 26 '18 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.