1

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> 
1
  • Split queries[0] on the = character, then print the value at index 1 of the resulting array. Commented Apr 30, 2021 at 17:38

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.