Skip to content

Instantly share code, notes, and snippets.

@annafergusson
Last active March 17, 2023 14:06
Show Gist options
  • Save annafergusson/f3c53a7e48b4c6d2ca6fb29320af2533 to your computer and use it in GitHub Desktop.
Save annafergusson/f3c53a7e48b4c6d2ca6fb29320af2533 to your computer and use it in GitHub Desktop.
Random redirect using iframes
  1. Under Google Drive, create a new Google Apps Script (you might have to look under "More" after you click the "New" button).
  2. Give your project a name e.g. Random redirect
  3. In the Code.gs tab, delete what is there and replace with this code.
  4. Create a new HTML file using the File menu, and call this "Index.html". Delete what is there and replace with this code.
  5. Replace the links to the Google forms in the Index.html code with your own :-)
  6. Save the project then select "Deploy as web app" from the "Publish" menu. Under "Who has access to the app" select "Anyone, even anonymous".

You should then get a "Current web app URL:" that looks something like this: https://script.google.com/macros/s/AKfycbw0WK1Ih65H9YoEJqnmrE7-uOpozkMdLrg0577kzAwek4dcyWqp/exec

You can then give people this URL (or use a URL shortener first to make it look nicer e.g. bit.ly, tiny.cc or embed it in an existing webpage), and it will randomly redirect them to one of the Google forms you've added to the code.

You can just use the Index.html file by itself without using Google Apps Script, for example if you have your own website.

function doGet() {
var output = HtmlService.createHtmlOutputFromFile("Index");
return output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
}
iframe {
display: block;
background: #ffffff;
border: none;
height: 100vh;
width: 100vw;
}
</style>
</head>
<body>
<iframe id='framey' frameborder = 0></iframe>
</body>
<script>
var webpages = [];
//---------------just change the webpage URLs
//the webpages need to hosted on https sites - if you want more than two just create a new line using the same code as below
//just change the link to the google form
webpages.push("https://docs.google.com/forms/d/e/1FAIpQLSfLfuayN7YVhFRzAWG87ArEeML6ltEUgDOB5rA9F0KIH2u_5w/viewform?usp=sf_link");
webpages.push("https://docs.google.com/forms/d/e/1FAIpQLSdjjtjCVrovMpGDH-9JlBwmXI5kNgK_EmJM1sfBRFDw3C4wWg/viewform?usp=sf_link");
//------------------------------------------
var random_page = webpages[Math.floor(Math.random()*webpages.length)];
document.getElementById("framey").src = random_page;
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment