Joe Hall
Joe Hall

Reputation: 29

How to read <input type="hidden"...> in JavaScript on subsequent page?

One first page: A form SUBMIT goes to a subsequent page.

VBscript can see the hidden value with ... Request("myName") ...

How do I do the same thing in JavaScript.

 alert(window.location.search);

or

 alert(window.top.location.search.substring(1));

return nothing.

Upvotes: 0

Views: 87

Answers (3)

Brigand
Brigand

Reputation: 86270

In your form, you have to have the method set to GET.

<form method="GET" action="somepage">
  <input type=hidden name=myHiddenValue />
</form>

Then on the next page, you can parse the search part of the url with a function like this.

function parseSearch(search, key) {
    search = search.substring(1), items=search.split("&");
    for (var i=0; i<items.length; i++) {
        var item = items[i], parts = item.split("=");
        if (parts[0] === key) {
            return parts[1] || true;
        }
    }
}
parseSearch(location.search, "myHiddenValue"); // returns the hidden value

live demo

Upvotes: 0

Roar
Roar

Reputation: 2167

<input type='hidden' id='hiddenId'/>

jQuery:

var value = $('#hiddenId').val();
alert(value);

Or

var value = document.getElementById('hiddenId').value;
alert(value);

Upvotes: 0

Roger Barreto
Roger Barreto

Reputation: 2284

Well, you dont. When you submit a form it sends the values to a server, and the "server-side" reads that in vbscript as Request (Requested). If you want to let the requested value accessible to the Javascript, your server-side (subsequent) page must write that Request data back to the client-side, in other worlds, you have to write the requested value directily in the HTML that will be send back to the client browser.

Ex: In your ASP (Server-Side Subsequent VBScript file) you should write

Response.Write ("<script type=""text/javascript"">alert('" & Request("Data") & "')</script>")

Upvotes: 1

Related Questions