So I am making a JS playground like JSFiddle. While adding the JavaScript functionality and calling different functions, it seems like the code is running from the page document root and not the iframe one.
$(document).ready(function()
{
var result = $("#result");
htmlVal = htmlEditor.getValue();
cssVal = cssEditor.getValue();
jsVal = jsEditor.getValue();
iframe = $("<iframe></iframe>");
result.append(iframe);
var iframeDoc = $(iframe.get(0).contentDocument) //iframe.contents();
bodyFrame = iframeDoc.find("body");
headFrame = iframeDoc.find("head");
cssFiddle = headFrame.append("<style id='cssEdit'>" + settings.JS + "\n" + cssVal + "</style>").children("#cssEdit");
jsFiddle = bodyFrame.append("<script id='jsEdit'>" + settings.JS + "\n" + jsVal + "</script>").children("#jsEdit");
$("#update").click(updateResult);
//setInterval(updateResult, 100);
});
function updateResult()
{
htmlVal = htmlEditor.getValue();
cssVal = cssEditor.getValue();
jsVal = jsEditor.getValue();
if(htmlVal.split("</body>").length == 2)
{
var bodyText = htmlVal.split("<body>")[1].split("</body>")[0];
bodyFrame.html(bodyText);
jsFiddle = bodyFrame.append("<script id='jsEdit'>" + settings.JS + "\n" + jsVal + "</script>").children("#jsEdit");
}
if(htmlVal.split("</head>").length == 2)
{
var headText = htmlVal.split("<head>")[1].split("</head>")[0];
headFrame.html(headText);
css = headFrame.append("<style id='cssEdit'>" + settings.JS + "\n" + cssVal + "</style>").children("#cssEdit");
} else
{
bodyFrame.html(htmlVal);
js = bodyFrame.append("<script id='jsEdit'>" + settings.JS + "\n" + jsVal + "</script>").children("#jsEdit");
}
jsFiddle.html(settings.JS + "\n" + jsVal);
cssFiddle.html(settings.CSS + "\n" + cssVal);
}