通过 Javascript document.write 显示 HTML 表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16657817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display an HTML form via Javascript document.write?
提问by user1789437
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?
以某种方式在跨不同服务器的许多网站内显示一个 html 表单?
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.
我有一个 HTML 表单,该表单将随着公司需求的变化而不断更新,并希望将它们从 IFRAME 调用中移除。
A different questions towards the same goal "How can I display off site content on a website and not use IFRAME"
针对同一目标的不同问题“如何在网站上显示站外内容而不使用 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>
采纳答案by Cem Demir
Okay, there is solution for you. Your embed code like this;
好的,有你的解决方案。你的嵌入代码是这样的;
<script>
var url = 'http://www.maxcashtitleloans.com/lmapp.js'
document.write('<script src="'+url+'"></scr'+'ipt>')
</script>
And http://www.maxcashtitleloans.com/lmapp.jslike this :
和http://www.maxcashtitleloans.com/lmapp.js这样的:
function ajaxex()
{
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.write(xmlhttp.responseText);
}
}
xmlhttp.open("GET","lmapp.htm",true);
xmlhttp.send();
}
ajaxex();
Thats work fine. And demo for you : http://commention.com/lmappjsexample/That solution like javascript proxy, you have to create a javascript file for render your html page.
那工作正常。并为您演示:http: //commention.com/lmappjsexample/像 javascript 代理这样的解决方案,您必须创建一个 javascript 文件来呈现您的 html 页面。
回答by flavian
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.
使用 JavaScript 创建 HTML 表单并将该脚本包含到所有其他使用相同来源的网站中。
Assume form.js
is the file you want to include in every website.
假设form.js
是您要包含在每个网站中的文件。
forms.js
表单.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.
现在只需包含forms.js
在任何其他网站上,但请确保src
对所有这些网站使用相同的内容,以便您可以使脚本保持最新。
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.
现在在每个网站上,他们都可以添加表单,company.form.append(someDiv)
当您更新脚本时,更新将在所有网站上可用。
回答by Martin
You can use simple jQuery:
您可以使用简单的 jQuery:
<script>
$('body').load(url);
</script>
回答by Justin808
well, a <script>
tag is for including javascript, not HTML. You want to look into ajax. You want to load the html file via ajax into a div.
好吧,<script>
标签用于包含 javascript,而不是 HTML。你想研究ajax。您想通过 ajax 将 html 文件加载到 div 中。
Alternatively, leave it as an iframe. Iframes are for including one page into another.
或者,将其保留为 iframe。iframe 用于将一个页面包含到另一个页面中。
EditThe example you included from the affiliate is meaningless for you. They are loading javascript that is generated programmatically from a PHP server side script based on input from client side cookies. You are trying to load en external html file, these are two different tasks.
编辑您从会员中包含的示例对您来说毫无意义。他们正在加载基于来自客户端 cookie 的输入从 PHP 服务器端脚本以编程方式生成的 javascript。您正在尝试加载外部 html 文件,这是两个不同的任务。
javascript in your HEAD tag
javascript 在您的 HEAD 标签中
<!-- Include the jquery library - never re-create the wheal -->
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
// Run once the page is loaded
$('#putFormHere').load('http://www.maxcashtitleloans.com/lmapp.html');
});
</script>
Replace your current iframe with this
用这个替换你当前的 iframe
<div id='putFormHere'></div>
Assumption
假设
I'm assuming that www.maxcashtitleloans.com
is the same domain as your current page. If not then they onlyway to do this is via an iframe. Javascript will not support cross-site scripting.
我假设这www.maxcashtitleloans.com
是与您当前页面相同的域。如果没有,那么他们唯一的方法就是通过 iframe。Javascript 将不支持跨站点脚本。
回答by Cem Demir
Script tags do not HTML rendering. You have to add HTML render to your html page. Maybe you can add this code to your html page. This code render your html codes on javascript.
脚本标签不进行 HTML 呈现。您必须将 HTML 渲染添加到您的 html 页面。也许您可以将此代码添加到您的 html 页面。此代码在 javascript 上呈现您的 html 代码。
document.write($(body).html());