How do I set the name of a window in ROR?
-
09-06-2019 - |
Question
How do I "name" a browser window in ROR, such that I can open a page in it later, from another (popup) window (using the target="name" html parameter)
Solution
You have to use JavaScript for this:
<script type="text/javascript">
window.name = "MyWindow";
</script>
Of course you could easily package this up into a Rails helper method. For example, in app/helpers/application_helper.rb
add a new method:
def window_name(name)
content_for(:window_name) do
"<script type=\"text/javascript\">window.name = \"#{name}\";</script>"
end
end
Next, in your layout file, add this line somewhere within the HTML <head>
element:
<%= yield :window_name %>
Finally, in your view templates, simply add a line like this (can be anywhere you want) to output the correct JavaScript:
<% window_name 'MyWindow' %>
OTHER TIPS
You could try below:
var x=window.open("", "myWindow");
var y="<head><title>my window</title></head><body>my window</body>";
x.document.write(y);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow