Skip to content

Commit

Permalink
Add JavaScript redirecting
Browse files Browse the repository at this point in the history
  • Loading branch information
Magicdream committed Feb 27, 2012
1 parent 4ceefc4 commit c677a4c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/facepalm/rails/controller/redirects.rb
Expand Up @@ -23,11 +23,22 @@ def redirect_from_iframe(url_options)
redirect_url = url_options.is_a?(String) ? url_options : url_for(url_options) redirect_url = url_options.is_a?(String) ? url_options : url_for(url_options)


logger.info "Redirecting from IFRAME to #{ redirect_url }" logger.info "Redirecting from IFRAME to #{ redirect_url }"


render( respond_to do |format|
:text => iframe_redirect_code(redirect_url), format.html do
:layout => false render(
) :text => iframe_redirect_html_code(redirect_url),
:layout => false
)
end

format.js do
render(
:text => iframe_redirect_js_code(redirect_url),
:layout => false
)
end
end
end end


# Generates HTML and JavaScript code to redirect user with top frame location # Generates HTML and JavaScript code to redirect user with top frame location
Expand All @@ -36,7 +47,7 @@ def redirect_from_iframe(url_options)
# @param target_url An URL to redirect the user to # @param target_url An URL to redirect the user to
# @param custom_code A custom HTML code to insert into the result document. # @param custom_code A custom HTML code to insert into the result document.
# Can be used to add OpenGraph tags to redirect page code. # Can be used to add OpenGraph tags to redirect page code.
def iframe_redirect_code(target_url, custom_code = nil) def iframe_redirect_html_code(target_url, custom_code = nil)
%{ %{
<html><head> <html><head>
<script type="text/javascript"> <script type="text/javascript">
Expand All @@ -50,6 +61,13 @@ def iframe_redirect_code(target_url, custom_code = nil)
</head></html> </head></html>
} }
end end

# Generates JavaScript code to redirect user
#
# @param target_url An URL to redirect the user to
def iframe_redirect_js_code(target_url)
"window.top.location.href = #{ target_url.to_json };"
end
end end
end end
end end
Expand Down

0 comments on commit c677a4c

Please sign in to comment.