简体   繁体   English

将JS变量注入haml模板

[英]Inject JS variable into a haml template

I'm trying use HTML5 localStorage with a Ruby haml template and need to be able to get the value of localStorage.getItem('myItem') to pass to a java applet (code stripped down): 我正在尝试使用带有Ruby haml模板的HTML5 localStorage,并且需要能够获取localStorage.getItem('myItem')的值以传递给java applet(代码被剥离):

- content_box("MyBox") do
  %object{:classid => "clsid:xxx"}
    %param{:name => "myItem", :value => "javascript:localStorage.getItem('myItem')"}
    %comment
      %EMBED{:myItem => "javascript:localStorage.getItem('myItem')"}
      %noembed

Is there a good way to do this? 有没有办法做到这一点? I can do something like: 我可以这样做:

:javascript
  document.write("<param name='myItem' value="+localStorage.getItem('myItem')+">"

but that's so ugly! 但那太丑了!

Note that this is an object I'm embedding, and need the value to be present before document_ready; 请注意,这是我嵌入的对象,需要在document_ready之前存在值; I cannot select the object and append the value to it on document_ready. 我无法选择对象并在document_ready上将值附加到它。 The only other way I can think of is to do an ajax submission to make the value a Ruby variable ahead of time, but that's really unnecessary. 我能想到的另一种方法是做一个ajax提交,使值提前变为Ruby变量,但这确实是不必要的。

Thanks! 谢谢!

Sometimes the only way that works is ugly. 有时唯一有效的方法是丑陋的。

IF your data is stored on the client, creating a server request/page/action just to get the data and pass it back in a different form straight back to the client is uncessessary, and arguably uglier. 如果您的数据存储在客户端上,创建服务器请求/页面/操作只是为了获取数据并以不同的形式将其直接传回客户端是不可能的,并且可以说是丑陋的。

Go with using javascript to add the <param> tag. 使用javascript添加<param>标记。

If the object depends on JavaScript anyway, you may as well just write the whole element with JavaScript instead of just the param. 如果对象依赖于JavaScript,你也可以用JavaScript而不仅仅是param来编写整个元素。 Then you can do it on document ready. 然后你可以在文件准备就绪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM