1

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.

1
  • Where exactly do you want to add the querystring? to the form action attribute or a link somewhere? Commented Jun 30, 2009 at 3:58

4 Answers 4

6

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I did go with the hidden fields and javascript combo and it worked out really well. Thanks.
I'm happy that worked out for you. Please mark as answer if you feel like it.
1

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

Comments

0

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.

2 Comments

jQuery is nice, but it will require jQuery :).
still not valid as you would need quotes around #fieldID
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.