How to redirect to another page, using the query that went to the previous page?

Asked

Viewed 64 times

1

I’m with an application that when the person registers, he plays to a page and after 3.5s he redirects to the other page, but I wanted him to redirect with the query I need, to already appear the registered person.

Code of the party that redirects to the successful registration page:

const db = await Database
    await createProffy(db, { proffyValue, classValue, classScheduleValues })

    let queryString = "?subject=" + req.body.subject
    queryString += "&weekday=" + req.body.weekday[0]
    queryString += "&time=" + req.body.time_from[0]


    return res.redirect('/success-proffy' + queryString)

The page comes with that query up there, as below:

print da primeira tela de sucesso

After 3.5 seconds it redirects to the Study page:

<script>
    setTimeout(() => {
      location.href = '/study'
    }, 3500)
  </script>

I need the query to go to that Study page too, just like it was for success.

NOTE: this setTimeOut is directly in the HTML of the successful page!!

2 answers

1


You can use the window.location.search.

If the current url is: http://foo.com/success-proffy?subject=FOO&weekday=1&time=32132132

window.location.search should return: ?subject=FOO&weekday=1&time=32132132

<script>
    setTimeout(() => {
        location.href = '/study' + window.location.search
    }, 3500)
</script>
  • 1

    Thank you very much, Lucas, it worked perfectly, you’re wild!!

  • just don’t forget to mark the question as answered

0

There is no way you can do any operation by the server after the res have been sent. The solution is only by the same browser

<script>
    setTimeout(() => {
      location.href = `/study${location.search}`
    }, 3500)
</script>

EDIT: this operation is safer with the search that the colleague quoted, since if there is no querystring returns "" as opposed to Slice -1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.