简体   繁体   中英

How do i print a variable defined in Javascript in Opal-rails

How can i access a variable in opal-ruby code that has been previously defined in javascript? For example in the following code how to fix the opal file.

index.html.erb

...
<script>
  var test = "hi"
</script>
...

index.js.opal

Document.ready? do
  puts test
end

Opal lets you use backticks to output any raw JavaScript that you don't want Opal to interpret, so this will output your test string containing "hi":

Document.ready? do
  puts `test`
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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