javascript - 如何将 javascript var 值传递给 Grails Controller ?

标签 javascript image grails window size

我找到了以下获取浏览器窗口大小的 javascript 代码,效果很好!

<script type="text/javascript">
<!--

 var viewportwidth;
 var viewportheight;

 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }

// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 else if (typeof document.documentElement != 'undefined'
    && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }

 // older versions of IE
 else
 {
       viewportwidth = document.body.clientWidth,
       viewportheight = document.body.clientHeight
 }
document.write('<p>Your viewport width is <b>'+viewportwidth+'x'+viewportheight+'</b>.</p>');
//-->
</script>

现在我需要将它传递给 Grails Controller ,以便我可以根据屏幕大小调整图像的大小。

构建使用:

<div align="center" valign="middle">
<img src="${createLink(controller:'chart', action:'buildChart')}" />
</div>

我该怎么做? 提前致谢!

最佳答案

如果您选择使用 jQuery,您可以编写一个返回图像的 Controller ,在您的 html 中是这样的:

<img id="picture"/>
....
....
<g:javascript>
    // Load something when DOM is fully loaded
    $(document).ready(function() {
       var width = $(window).width()
       var height = $(window).height()
       $('img#picture').attr('src','${createLink(controller: 'image', action: 'resize')}?width='+width+'&height='+height)
   })
</g:javascript>
....
</body>

还有一些 Controller 代码:

class ImageController {

  def resize = {
     def width = params.int('width')
     def height = params.int('height')
     // ... resize your image and return your image in the output stream
  }
}

以上内容完全超出了我的想象,因此您必须填写空白:-)

祝你黑客愉快。

关于javascript - 如何将 javascript var 值传递给 Grails Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4442143/

相关文章:

javascript - 清除并添加新类别?

Java:从图像中裁剪出具有特定颜色的区域

image - 为什么 Google PageSpeed 提示 Retina 图像?

grails - 检查用户是否在主页上

javascript - 想在页面 url 更改时设置警报

javascript - 工厂的 es6 语法

javascript - jquery SlideDown/slideUp 与多个 div

c++ - opencv 从缓冲区读取 jpeg 图像

java - 将 Grains 添加到现有项目

hibernate - Grails 2 StaleObjectStateException