0

This is what i used to do in html.erb earlier:

my markup
<script type="text/javascript">
    var somevalueFromInstance = "<%= @value_from_instance %>";
    // code directly using the someValueFromInstance
</script>
markup continues..

mostly i will place the script tag in a content_for block.

Now when i want to do this unobtrusive, i cannot have any script tags in the html.erb. The script lies in a javascript file. But how do i transport the @value_from_instance to the javascript file?

Is storing it in a data-attribute the only option to do this?

Also i want to do this on page load and not in an ajax request for this purpose

1 Answer 1

1

The data-* attribute is do to that. It's the must simplest way to define your args or you can create some variable in a script tag in your HTML directly.

Sign up to request clarification or add additional context in comments.

5 Comments

no limit in theory. You can put your array in content of script tag instead. Like do by javascript templating by example.
sorry.. i do not understand. can u please elaborate?
add your variable directly in your HTML and only variable. Without function.
yes and no. Yes because there are no code not reuse and No because there are some code. You can't be complete unobstrusive
well.. not convincing enough for my finicky client, who has a problem with script in markup :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.