I am loading custom html and javascript into an iframe on a website. Up to html, css and javascript everything works fine. However, loading a framework into the iframe does not work.
So I am wondering if there is a way to use frameworks inside an iframe with custom set html/javascript.
Here is a simplified version of the problem. Trying to use jquery after adding it to the head gives an error:
// First: Set HTML of an iframe
examplePage = '<!DOCTYPE html><html><head></head><body><h1 id="test-id">test content</h1></body></html>'
let testFrame = $('#testFrame').get(0).contentDocument;
testFrame.body.innerHTML = examplePage;
// Trying to add Jquery
$head = $(testFrame).find('head');
$head.append('<scr'+'ipt src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/p5.min.js"></scr'+'ipt>');
// Trying to use jquery in iframe
customScript = '<scr'+'ipt>$("#test-id").click(function(){console.log("Is this working?!")});</scr'+'ipt>';
$head.append(customScript);
<iframe id="testFrame"></iframe>
Here is a link to the jsfiddle: https://jsfiddle.net/henkdedikzak/x3qn4ybL/