Display an HTML form via Javascript document.write?

user1789437 picture user1789437 · May 20, 2013 · Viewed 11.2k times · Source

If I am way off on how to employ this code then please forgive me, but is it possible to use something like

var url = 'http://www.maxcashtitleloans.com/lmapp.html'
document.write('<script src="'+url+'"></scr'+'ipt>')

to somehow display an html form inside many websites across different servers?

I have a single HTML form that will be continually updated as the needs of the company change, and would like to get them off of IFRAME calls.

A different questions towards the same goal "How can I display off site content on a website and not use IFRAME"

I know of an affiliate marketing company that uses

<script type='text/javascript'> 
    var inputOptions = { 
        UserID: '35696', 
        Product: 'payday', 
        ProductTemplate: 'lights', 
        Server: 'https://altohost.com/', 
        mobileDevices: true, 
        parseDefaultValue: true, 
        visitor: { 
            referrer: (document.cookie.match("rfrrr[\r\n\t ]*=[\r\n\t ]*(.*?)(;|$)") || [,''])[1],   
            subaccount: (document.cookie.match("src[\r\n\t ]*=[\r\n\t ]*(.*?)(;|$)") || [,''])[1], 
            keyword: (document.cookie.match("kwrd[\r\n\t ]*=[\r\n\t ]*(.*?)(;|$)") || [,''])[1], 
            clickid: (document.cookie.match("clcid[\r\n\t ]*=[\r\n\t ]*(.*?)(;|$)") || [,''])[1] 
        },     
    }; 
    document.write('<scr'+'ipt type="text/javascript" src="https://altohost.com/system/applicationforms/init.php?vn=inputOptions"></scr'+'ipt>'); 
</script> 

Answer

flavian picture flavian · May 20, 2013

I'd propose a slightly different approach.

Use JavaScript to create the HTML form and include that script into all other websites using the same source.

Assume form.js is the file you want to include in every website.

Live DEMO

forms.js

var company = {};// Avoid name clashes!!!

company.form = function() {
    this.render();
};

company.form.prototype.render = function() {
    var url = "blablabla";
    this.form = document.createElement("form");
    this.form.setAttribute("method", "post");
    this.form.setAttribute("name", "company-specialform");
    this.form.setAttribute("action", url);

    var input = document.createElement("input");
    input.setAttribute("type", "text");
    input.setAttribute("value", "test");

    var submit = document.createElement("input");
    submit.setAttribute("type", "submit");
    submit.setAttribute("value", "submit");
    this.form.appendChild(input);
    this.form.appendChild(submit);
    var that = this;
    this.form.onsubmit = function(event) {
        that.submit.call(that, event);
    };
};

company.form.prototype.submit = function(event) {
    event.preventDefault(); // if needed
    alert(" Custom submit was called");
};

company.form.prototype.getForm = function() {
    return this.form;
};

company.form.append = function(container) {
    var form = new company.form();
    container.appendChild(form.getForm());
};

var target = document.getElementById("container");

company.form.append(target);

Now simply include forms.js on any other website, but make sure you use the same src for all of those websites, so you can keep the script up to date.

Now on every of those website, they can add the form with company.form.append(someDiv) and when you update the script the update will be available on all websites.