将参数传递给window.onload函数

时间:2017-06-20 21:14:15

标签: javascript html spring spring-boot

如何使用ModelAndView中的参数将其传递给HTML文件中的javascript中的window.onload函数?

控制器文件:

@RequestMapping(value = "/admin/printtext")
public ModelAndView printtext() {
    ModelAndView modelAndView = new ModelAndView("printtext", "showBackLink", false);
    String name = "name0";
    modelAndView.addObject("name", name);
    return modelAndView;
}
HTML文件中的

脚本:

<script type="text/javascript">
    window.onload = function (${name}) {
        document.write('text')
    }
</script>

1 个答案:

答案 0 :(得分:0)

您将其插入错误的位置。

<script type="text/javascript">
var serverData = "${name}";
window.onload = function () {
    document.write(serverData); // don't actually use document.write()
}
</script>