2009-06-30 44 views
1

使用javascript,如何向查询字符串添加一些数据?查询字符串Javascript

基本上我想将window.screen.height和window.screen.width info添加到查询字符串中,以便我可以通过电子邮件将其与其他登录信息一起发送。

或者,我将如何使用相同的数据填充两个隐藏字段,表单正在提交中,以便我可以从中提取它?

谢谢,R.

+0

你究竟在哪里添加查询字符串?表单操作属性或链接的地方? – 2009-06-30 03:58:44

回答

6

我认为后面的选项会更容易实现。例如,如果你有隐藏的字段是这样的:

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

然后JavaScript的将是:

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

为隐藏字段,给每个字段然后一个ID为此

$("#fieldID").attr("value") = someVal UE;

更新:对不起,我看到了查询,这让我想到了jQuery。

+0

jQuery很好,但它需要jQuery :)。 – 2009-06-30 04:00:30

+0

+1回到0 – 2009-06-30 04:05:19

0

如果你想将它添加到位置在图书馆不可知的方式,你可以做到以下几点:

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

当然,这假设你没有在查询字符串u或h参数已经。