如何将 javascript var 值传递给 Grails 控制器?

发布于 2024-10-07 06:41:46 字数 1358 浏览 1 评论 0 原文

我找到了以下 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 控制器,以便我可以根据屏幕尺寸调整图像大小。

它是使用以下内容构建的:

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

我该怎么做? 提前致谢!

I have found the following javascript code to get browser window size and it works great!

<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>

Now I need to pass it to a Grails controller so I can resize a image according to screen size.

which is built using:

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

How can I do so?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2024-10-14 06:41:46

如果您选择使用 jQuery,您可以编写一个返回图像的控制器,在 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>

还有一些控制器代码:

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
  }
}

上面的内容完全是我的想法,所以你必须填写空白:-)

快乐的黑客。

If you choose to use jQuery you could write a controller that returns your image, something like this in your 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>

And some controller code:

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
  }
}

The above is entirely off the top of my head, so you have to fill in the blanks :-)

Happy hacking.

美人迟暮 2024-10-14 06:41:46

createLink 标记将允许在调用控制器操作时传递参数。让您的 javascript 确定这些值,然后将它们插入可由 createLink 标记引用的隐藏变量中。 jQuery 有一些很棒的选项可以轻松访问 DOM 元素。

例如,您的 img 元素可能如下所示:

我们使用类似的技术通过 Grails 和 jQuery 构建 remoteFunction 方法调用。您可能需要调整上面的示例,它未经测试。

createLink tag will allow params to be passed on that call to the controller action. Have your javascript determine the values and then plug them into hidden variables that can be referenced by the createLink tag. jQuery has some great options for easily accessing DOM elements.

For example, your img element could look like this:
<img src="${createLink(controller:'chart', action:'buildChart', params:\"['vpWidth' : document.getElementById(\'vpWidth\').value, 'vpHeight': document.getElementById(\'vpHeight\').value]\")}" />

We use a similar technique to build remoteFunction method calls with Grails and jQuery. You may have to adjust the above example, it is untested.

早茶月光 2024-10-14 06:41:46

使用 javascript 添加包含所需数字的隐藏表单字段。在 grails 控制器中,使用 request.getParameter("fieldName") 以字符串形式检索值,然后转换为 int 并调整大小。

Use javascript to add hidden form fields containing the numbers you want. From with in the grails controler use the request.getParameter("fieldName") to retrieve the values in the form of a string then convert to an int and do your resizing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文