1

私はこの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 変数に渡す方法は?

4

1 に答える 1

6

Perlはサーバー上で実行されています。クライアント(ブラウザ)に送信されるテキストを出力します。

次に、ブラウザはそのテキストとHTMLおよびJavaScriptを解釈します。

新しいHTTPリクエストを作成せずにデータをPerlに戻すことはできません。

オプションは次のとおりです。

  • JavaScriptをPerlに戻そうとするのではなく、JavaScriptで処理を終了します
  • Ajaxを使用して新しいHTTPリクエストを作成します
  • location.hrefクエリ文字列で渡されたデータを使用して新しいページをロードするように設定します
  • 現在のロジックを使用せずに(指定されていない)目標を達成する方法を見つけます(たとえば、CSSメディアクエリを使用して、ブラウザーのサイズに基づいてページのスタイルを変えることができます)。
于 2012-05-08T08:28:42.653 に答える