Opening a Link in a New Window within an iFrame

心不动则不痛 提交于 2019-12-19 10:43:10

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!