我有这个 javascript 函数:
print <<"EOT";
<script type="text/javascript">
function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
window.alert( 'Width = ' + myWidth );
window.alert( 'Height = ' + myHeight );
}
</script>
EOT
print "<body onload='alertSize()'>";
print "</body>";
my $windowHeight = $q->param('myHeight');
my $windowWidth = $q->param('windowwidth');
print "<$windowHeight><$windowWidth>";
如何将 javascript 函数的值传递到我的 Perl 变量?
最佳答案
您的 Perl 正在服务器上运行。它输出一些发送到客户端(浏览器)的文本。
然后浏览器解释该文本、HTML 和 JavaScript。
如果不发出新的 HTTP 请求,则无法将数据传回 Perl。
您的选择包括:
- 使用 JavaScript 完成处理,而不是尝试将其传递回 Perl
- 使用 Ajax 发出新的 HTTP 请求
- 设置
location.href
以使用查询字符串中传递的数据加载新页面 - 想办法在不使用当前逻辑的情况下实现您的(未指定的)目标(例如,您可以使用 CSS 媒体查询根据浏览器尺寸设置不同的页面样式)。
关于javascript - 如何将 javascript 值分配给 Perl 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10495092/