0

I am trying to write to my iframe on keyup but it does not update. I will run through the code I am using to try to achieve this.

First, I am grabbing the original HTML that I would like on the iframe (through AJAX) and writing that to the iframe as so:

var base_tpl = base_html; //get base template html.
var iframe = document.querySelector('#output iframe'),
iframe_doc = iframe.contentDocument;
iframe_doc.open();
iframe_doc.write(base_tpl);
iframe_doc.close();

After that I am attaching document.querySelector to the relevant text areas that will serve as inputs for the changes that I want to show up on the corresponding elements within the iframe. Here is how I am doing that:

var the_title = document.querySelector('#the_title2 textarea'),
    the_subheading = document.querySelector('#the_subheading2 textarea'),
    the_image_url = document.querySelector('#the_image_url2 textarea'),
    the_description = document.querySelector('#the_description2 textarea'),
    the_signup_button_text = document.querySelector('#the_signup_button_text2 textarea'),
    the_email_capture_text = document.querySelector('#the_email_capture_text2 textarea'),
    the_email_capture_button_text = document.querySelector('#the_email_capture_button_text2 textarea');
the_css = document.querySelector('#the_css2 textarea');
var editors = [the_title, the_subheading, the_image_url, the_description, the_signup_button_text, the_email_capture_text, the_email_capture_button_text];

I then attach these to on keyup listeners as such:

// Attaching the onkeyup Event
editors.forEach(function(editor, i, arr) {

    editor.addEventListener('keyup', function() {

        // The function that'll prepare the code and inject
        // into the iframe.
        //alert(the_title.value);

        render();

    }, false);

});

Notice the render function, it works like this:

var render = function() {
    var source = prepareSource();

    var iframe = document.querySelector('#output iframe'),
        iframe_doc = iframe.contentDocument;
    console.log(source);
    iframe_doc.open();
    iframe_doc.write(source);
    iframe_doc.close();
};

Prepared source is the function that puts together the newly inputted elements. The methodology behind it can be seen here:

var prepareSource = function() {
    var title = the_title.value,
        subheading = the_subheading.value,
        image_url = the_image_url.value,
        description = the_subheading.value,
        signup_button_text = the_image_url.value,
        email_capture_text = the_subheading.value,
        email_capture_button_text = the_image_url.value,
        css = the_css.value,
        src = '';

    src = base_tpl;
    // Title
    src = src.replace("<div id='the_title'><h3><?php echo $page->title; ?></h3></div>", "<div id='the_title'><h3>" + title + "</h3></div>");
    // Subheading
    src = src.replace("<div id='the_subheading'><h3><?php echo $page->subheading; ?></h3></div>", "<div id='the_subheading'><h3>" + subheading + "</h3></div>");
    // Image URL
    src = src.replace("<div id='the_image_url'><h3>", image_url + "</h3></div>");
    // Description
    src = src.replace("<div id='the_description'><h3>", description + "</h3></div>");
    // Signup Button Text
    src = src.replace("<div id='the_signup_button_text'><h3>", signup_button_text + "</h3></div>");
    // Email Capture Text
    src = src.replace("<div id='the_email_capture_text'><h3>", email_capture_text + "</h3></div>");
    // Email Capture Button Text
    src = src.replace("<div id='the_email_capture_button_text'><h3>", email_capture_button_text + "</h3></div>");

    // CSS
    css = '<style>' + css + '</style>';
    src = src.replace('</head>', css + '</head>');

    return src;
};

My question is of whether i am doing something wrong here. The main issue is that the prepared source does not seem to be working at all. Nothing changes on the iframe on keyup. I have tested this with console.log. It does not seem to be affected at all.

I would appreciate any help here.

Cheers!

Tapha.

EDIT: Please ignore the elements after subheading in the 'prepared source' section - i have only done the title and subheading portions for testing purposes.

Tapha
  • 1,491
  • 3
  • 17
  • 32
  • Hey friend! I do not have time to answer your question but I'd suggest to cut it up and show just the essential part of it. This is so hard to read. –  Dec 26 '14 at 18:16
  • Hi @JuanRocamonde, yes i would, but the problem is that the issue could be anywhere :) so i did not want to leave anything out, lest it be pointless posting here. – Tapha Dec 26 '14 at 18:31
  • @Tapha If possible, can post `html` ? , create stacksnippets / http://jsfiddle.net ? Thanks – guest271314 Dec 26 '14 at 18:35
  • @guest271314 This is, unfortunately, very difficult to reproduce in part. – Tapha Dec 26 '14 at 18:40
  • http://stackoverflow.com/questions/22740665/using-textareas-to-display-live-results-in-iframe-using-jquery-add-separate-te/ ? – guest271314 Dec 26 '14 at 18:57
  • look into using `postMessage` API. Also make sure iframe isn't from another domain – charlietfl Dec 26 '14 at 22:03

0 Answers0