flavour404
flavour404

Reputation: 6312

Query String Javascript

Using javascript, how do I add some data to the query string?

Basically I want to add the window.screen.height and window.screen.width info to the query string so that I can then email it with the other login info.

Alternatively, how would I populate a couple of hidden fields with the same data, a form is being submitted so I could pick it up from there?

Thanks, R.

Upvotes: 1

Views: 381

Answers (4)

seth
seth

Reputation: 37297

If you wanted to add it to the location in a library agnostic manner, you could do the following:

var query = window.location.search;
var sep = "?";
if (query) {
   sep = "&";
}
window.location = window.location + sep + "h=" + 
    window.screen.height + "&w=" + window.screen.width;

This of course assumes that you don't have w or h parameters in the query string already.

Upvotes: 0

gargantuan
gargantuan

Reputation: 8944

for hidden fields, give each field an ID then do this

$("#fieldID").attr("value") = someValue;

UPDATE: Sorry, I saw Query and that made me think jQuery.

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40527

You can also use jquery.query.js to play with querystring.

Upvotes: 1

Bravery Onions
Bravery Onions

Reputation: 519

I think that the latter option would be easier to implement. For example, if you have the hidden fields like this:

...
<input type="hidden" name="screenheight" id="screenheight" value="" />
<input type="hidden" name="screenwidth" id="screenwidth" value="" />
...

Then the JavaScript would be:

<script type="text/javascript">
document.getElementById('screenheight').value = window.screen.height;
document.getElementById('screenwidth').value = window.screen.width;
</script>

Upvotes: 6

Related Questions