Google Script - Sidebar button keeps opening a new tab

余生长醉 提交于 2019-11-26 12:48:48

问题


I\'m creating an extremely basic script to assist me in one of my google spreadsheets.

I\'ve successfully got a sidebar showing, with a few buttons (which function). However, whenever I click on one of those buttons, it also opens up a new tab, with a URL something like: \"https://n-lx3mdv5ls3mdgglsq226llilxd2m4owxy72y3fy-1lu-script.googleusercontent.com/userCodeAppPanel?\"

Even if the button is left without any functionality, this still occurs.

Here\'s an example of the html:

<!DOCTYPE html>
<html>
  <head>
    <base target=\"_top\">
    <link rel=\"stylesheet\" href=\"https://ssl.gstatic.com/docs/script/css/add-ons1.css\">
    <!-- The CSS package above applies Google styling to buttons and other elements. -->

    <style>
    
    </style>
  </head>
  <body>
    <div class=\"sidebar\">
      <form>
       <div class=\"block\" id=\"buttons\">
          <button id=\"unindent\">Unindent</button>
          <button id=\"indent\">Indent</button>
          <button id=\"asdgdgasg\">Test</button>
        </div>
      </form>
    </div>
  </body>
</html>

How can I stop this new tab from opening every time?


回答1:


Issue:

  • <button> type, if not specified defaults to type=submit. So, The form automatically submits the data to the server. This is not preferable in a iframe.

Solution:

  • Explicitly specify button type as button. <button type='button'>
  • Use event.preventDefault() to prevent automatic form submission.

References:

  • Button#Default
  • Sample Form



回答2:


When I saw your issue, I thought that this is due to <form>. So I proposed the following modifications.

  1. Modify <form> to <form onsubmit="event.preventDefault()">.

  2. Modify <base target="_top"> to <base target="_self">.

  3. Remove <base target="_top">.

Note:

  • I think that @TheMaster's answer is more useful information.

References:

  • Event.preventDefault()
  • The Document Base URL element


来源:https://stackoverflow.com/questions/53825069/google-script-sidebar-button-keeps-opening-a-new-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!