question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Beginner trying to redirect users to an HTML page instead of Appscript JSON result

See original GitHub issue

Hello Author,

I am a beginner and i didn’t understand the note saying :

" If you want to intercept the submit event so the user isn’t redirected to the webapp, you can do this by attaching a JavaScript event listener to the form submission and creating the POST request yourself "

I dont want the user to have an “https://script.googleusercontent.com/macros/echo?user_content_key=....etc

and a JSON response like this :

image

The JS in codepen is the following. I don’t really understand were to put it. Can you help me please ?

Before all i have to say that i was more a lowcoder (with appsheet and Excel VBA)

Thank you by advance

window.addEventListener("load", function() { const form = document.getElementById('my-form'); form.addEventListener("submit", function(e) { e.preventDefault(); const data = new FormData(form); const action = e.target.action; fetch(action, { method: 'POST', body: data, }) .then(() => { alert("Success!"); }) }); });

(i created a Github account just because of this. A great opportunity 😃 )

El Moumen Y.

@levinunnink @meandavejustice @shadichy

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
shadichycommented, Sep 25, 2022

I am also looking for the solution. Above responses are not working.

Detail please

1reaction
shadichycommented, Sep 9, 2022

@elmoumenyassine First, assign an id to your form (<form id="yourFormID" ...) Next, add

window.addEventListener("DOMContentLoaded", function() { // 'window.onload = function...' also works 
  const yourForm = document.getElementById("yourFormID");
  yourForm.addEventListener("submit", function(e) { // 'yourForm.onsubmit = function...` also works too
    e.preventDefault(); 
    const data = new FormData(yourForm); 
    const action = e.target.action; 
    fetch(action, { 
      method: 'POST', 
      body: data, 
    }).then((response) => {
      response.json();
    }).then((data) => {
      if (data.result == "success") {
        // finished, you can do whatever you want here
        alert("Success!")
      }
    })
  })
});

to your js source or HTML source (inside script tag)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I redirect my user after the Google Apps script runs?
I have tried deleting the .createTextOutput and replace it with the window.location.replace("http://www.google.com"); and I ...
Read more >
Google Apps Script - Authorization in a cross-origin iframe
How to embed your Google Apps Script in an iframe when a user needs to authenticate it first before it can work.
Read more >
HTML Service: Create and Serve HTML | Apps Script
To create a web app with the HTML service, your code must include a doGet() function that tells the script how to serve...
Read more >
10 Features Your Streamlit ML App Can't Do Without
Much has been written about Streamlit killer data apps, and it is no ... from the beginning, and we want the user's downtime...
Read more >
How to redirect to another webpage using JavaScript
location.href: It is used to set or return the complete URL of the current page. · location.replace(): It is used to replace the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found