gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:51:55 27 4
gpt4 key购买 nike

我找到了以下获取浏览器窗口大小的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com