javascript - 如何将 javascript 值分配给 Perl 变量

标签 javascript html perl

我有这个 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/

相关文章:

javascript - 可以从 KnockoutJS 模型和 View 中删除事件处理吗?

javascript - 如何在 Javascript 中循环函数?

javascript - 对数据库中的数据设置多个过滤器

javascript - 为什么 (new Date() == new Date()) 为假,而 (Date() == Date()) 为真?

javascript - 使用 Chrome 开发者工具获取 XXX-xsrfstatemanager.js 文件的启动器

javascript - 动态表单框

php - 如何在 drupal 7/自定义内容类型/中设置此 View 的样式?

multithreading - Perl:使用多线程构建复杂对象树

perl - 替换大文件中的最后一个字符

Perl 在 void 上下文中对私有(private)变量的无用使用