Skip to content

Instantly share code, notes, and snippets.

@liddiard
Created May 19, 2015 18:26
Show Gist options
  • Save liddiard/8992b4d54a9608480068 to your computer and use it in GitHub Desktop.
Save liddiard/8992b4d54a9608480068 to your computer and use it in GitHub Desktop.
Some code I was playing with to inject JavaScript into an iframe
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<style>
iframe {
width: 960px;
height: 640px;
}
</style>
<iframe id="navigator" src="http://example.com/"></iframe>
<button id="switch">Switch</button>
<script>
$(document).ready(function(){
var change = document.getElementById('switch');
change.addEventListener('click', changeSrc, false);
function changeSrc() {
var nav = document.getElementById('navigator');
nav.src = "http://uclabruins.com/";
return false;
}
var nav = $('#navigator');
$('*', nav.contents()).hover(
function() {
$(this).css('background-color', 'yellow');
},
function() {
$(this).removeAttr('style');
}
);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment