I`m looking to create something similar to the Digg/Stumbleupon bar that is fixed to the top of a newly opened page.
In my application I display a bunch of links to external sites. No when someone clicks one of them, I would want to them to be opened within a new tab with my logo/bar up top of the page.
I would assume that this can be done using iframes.
However I don`t know what would be the easiest way to pass the URL parameter for iframe src to be rendered onto the new tab.
Any feedback is deeply appreciated.
You'll have to point your links to a Rails action, passing the external URL as a parameter.
So instead of:
= link_to "http://rubyonrails.org/"
# => <a href="http://rubyonrails.org/">
You would link to:
= link_to open_url_path, url: "http://rubyonrails.org/"
# => <a href="/open_url?url=http%3A%2F%2Frubyonrails.org%2F">
Then you can pass the url from your controller to the view:
def open_url
@url = params[:url]
end
And render the HTML containing your toolbar and iframe:
<div id="toolbar" />
<iframe src="<%= @url %>" />
来源:https://stackoverflow.com/questions/11352072/opening-a-link-in-a-new-window-within-an-iframe