yasuperu
yasuperu

Reputation: 77

Passing JavaScript data value between 2 HTML pages

I'm trying to pass a value from one HTML document to the other, and this works, but when it's printing out the value, it's adding the name tag text= in front of it. Is there a way to get rid of the text= and just showing the entered value?

here is the form of the first page: pageOne.html

<form action="pageTwo.html" method="get">
    <input type="text" name="text" />
    <input type="submit" value="submit" />
</form>

and here the script of the second page: pageTwo.html

<script>
    var queryString = decodeURIComponent(window.location.search);
    queryString = queryString.substring(1);
    var queries = queryString.split("&");
    document.write(queries[0]);    
</script> 

Upvotes: 1

Views: 54

Answers (1)

ptts
ptts

Reputation: 2086

You can use URLSearchParams.

GET /pageTwo.html?text=hello

<script>
  var urlParams = new URLSearchParams(window.location.search)
  var textValue = urlParams.get('text')
  console.log(textValue)
</script>

Result: hello

Upvotes: 3

Related Questions