DEV Community

Calin Baenen
Calin Baenen

Posted on

What would everyone think of a `window.post` variable in JavaScript?

Okay, hear me out because this case may or may not be niche.

The Issue

So, PHP has $_POST, which can access posted form data, which is nice for things that work server-side.
But, what if I wanted my HTML to be a local file, which could run offline; now, let's say I had a form and I wanted to send the data to another file:

<form action="./process_info.html" method="post">
  <!-- ... -->
</form>
Enter fullscreen mode Exit fullscreen mode

I feel like we should have something to PHP $_POST.

The Solution

My solution is window.post, which would either be a generic object, or a map of sorts β€” or, in classic JavaScript fashion, it could be its own class.
Something like:

<script>
  if(window.post.get("age") < 18) { /* Generic under-18 code. */ }
  else { /* Other code. */ }
</script>
Enter fullscreen mode Exit fullscreen mode

What Exists Currently

Looking at a StackOverflow post I found, one solution seems to be to override the behavior of the submit code and use sessionStorage then redirect; however, there is no real means of (more) effectively accomplishing this.
As GetFree says in a reply to the accepted answer, this is something that would be perfectly viable; not only would this work because the browser already knows the data, but also each local HTML file is treated as its "own server" (kind of), so, in theory, data could be passed to a local file like it could a file on a server, or at least I believe so.

What are your thoughts?

Do you (all) think this is something that could provide benefit?
Would it be worth adding this for local HTML apps?

Thanks for reading!
Cheers!

Top comments (0)