Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

javascript - Google Script - Sidebar button keeps opening a new tab

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...