Write a a few lines of javascript code that can detect if a iframe is being clicked and when it is clicked then it redirects user to another website like www.youtube.com/example

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%
Write a a few lines of javascript code that can detect if a iframe is being clicked and when it is clicked then it redirects user to another website like www.youtube.com/example
Expert Solution
iframe

URL redirect in iframe embedded surveys will be "trapped" inside the iframe.

In other words, the new website will only show up inside the iframe window, which is usually not what users desire.

Thankfully, there are a couple of ways around this! Both options involve removing the URL Redirect action and using some custom JavaScript instead to achieve the desired behavior.

Open in a new window:

The first option is to open the new website in a new window, which would accomplish the goal of "breaking out" of the iframe.

A problem with this solution is that most modern browsers automatically block pop-up windows, so your respondents are not likely to see the new window you tried to open.

If you'd like to use this first solution, you may wish to include some text on the final page of your survey letting the respondent know that you have opened a new window in their browser.

In case the window was blocked, often the user is given an option to override that and allow the window if they choose. Here is an example piece of JavaScript that would open a website in a new window:

redirect
$(function() {
  if ('app' !== $('body').attr('id')) {
    window.open('http://www.youtude.com');
}

Place this JavaScript in a JavaScript Action in place of the URL Redirect Action.

To add a new JavaScript Action, start in the Build tab on the page you would like to have this behavior:

1. Click Add New Action at the bottom of the page.

2. Select JavaScript.

3. Paste the above code in the JavaScript field, change the highlighted URL to the website you would like to redirect to and click Save Action.

Note: JavaScript Actions already include an opening and closing script tag, so you do not need to add them.

Redirect the page containing your iframe embed

The second option would be to redirect the page where you have your iframe embedded, known as the "parent" page.

Modern browsers will prevent an iframe from changing the location of its parent page for security reasons.

Your iframe embed code and JavaScript code will need to be modified to allow this behavior.

To modify your iframe embed code, you will add the following attribute inside the opening iframe tag:

sandbox="allow-top-navigation allow-scripts allow-forms"

As an example, here is a standard iframe embed code, with the necessary modifications highlighted to illustrate where they should be added:

<iframe src="http://www.Example.com/s3/1234567/surveyname" frameborder="0" width="700" height="500"
 style="overflow:hidden" sandbox="allow-top-navigation allow-scripts allow-forms"></iframe>

And inside your survey, instead of using the javascript to "open in a new window," you would use this bit of javascript instead.

$(function() {
  if ('app' !== $('body').attr('id')) {
    window.top.location.href = 'http://www.youtude.com';
  }
});
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
JQuery and Javascript
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education