ripper234
ripper234

Reputation: 230028

Can you read the flash from javascript?

Play's flash is a "temporary storage mechanism that is kept for the duration of the next request", using cookies.

Is it possible to read the flash from javascript?

I want to implement a generic client-side notification mechanism, and also access that mechanism from the server. What I imagine is for any controller to do:

flash.put("notification", "You have been notified");

And then for my main.js to check if the "notification" flash exists, and if it does display it above (similar to Stack Overflow's notification mechanism).

However, I suspect that flash might be encrypted in a way that the client side cannot read it. I haven't found any code samples that show how to read it from the client side JS (not in a template). Is this possible directly?

As I'm writing this question, I'm actually coming up with a way to implement this: I can put a hidden div in the main template that all my views extend, and access that div from JS. Does it make sense? Is there a simpler approach?

Upvotes: 1

Views: 739

Answers (2)

Seb Cesbron
Seb Cesbron

Reputation: 3833

For me you have two solutions

  1. In your view call a javascript function with the value as an argument, then in javascript you can store it in an object for later use
  2. The flash cookie isn't encrypted, it is sign, so you can read it but you can't modify. Use a tool like firebug to see the content of your flash cookie

Upvotes: 0

Gelin Luo
Gelin Luo

Reputation: 14373

I don't think it's possible to use JavaScript to read flash, even via the cookie. That cookie is actually encrypted with a security seeds (application.secret) configured in your application.conf.

However it is possible to implement what you want.

  1. Create a tag to export flash variables into Javascript object.
  2. Call that tag in your view.
  3. Your main.js use that exposed variable to do whatever it want.

Upvotes: 1

Related Questions