简体   繁体   English

如何将网页从文本区域发送到iframe

[英]How to send a web page from a textarea to iframe

So I have an iframe that's supposed to hold the rendered code from a textarea once a button is pressed, but I'm not sure how to do this in javascript or jquery. 因此,我有一个iframe,一旦按下按钮,该iframe应该保存来自textarea的呈现的代码,但是我不确定如何在javascript或jquery中执行此操作。 I'm aware of how to send a specific site with a URL to display inside a webpage, but for some reason when I try to render the textarea and send it to the iframe, it doesn't work. 我知道如何发送带有URL的特定站点以在网页内显示,但是由于某些原因,当我尝试渲染textarea并将其发送到iframe时,它不起作用。

this is my iframe: 这是我的iframe:

<iframe id="outputIframe"></iframe>

this is the function I wrote to send contents from textarea editor (this works just fine with a but not with ): 这是我编写的用于从textarea编辑器发送内容的函数(使用a可以正常使用,但不能使用):

function openIframe() {
        var e = document.getElementById('outputIframe');
        var editorHTML = editor.getValue();
        e.document.innerHTML = editorHTML;
    }

So the editor (codemirror) holds the HTML code which users write, and then it should output in the 'outputIframe' iframe element when users press a button. 因此,编辑器(codemirror)保存用户编写的HTML代码,然后在用户按下按钮时将其输出到'outputIframe'iframe元素中。 This is similar to the "Try it" sections of w3schools. 这类似于w3schools的“尝试”部分。

    function openIframe() {
            var editorHTML = editor.getValue();
            var iframe = document.getElementById('outputIframe');
            iframe.contentWindow.document.open();
            iframe.contentWindow.document.write(editorHTML);
            iframe.contentWindow.document.close();
        }

http://jsfiddle.net/tintucraju/2Lsr9ju9/ http://jsfiddle.net/tintucraju/2Lsr9ju9/

Using jquery you can type: 使用jquery,您可以输入:

$("iframe").contents().find("body").html(yourHTML);

Important to say, this only works if iframe and your parent window are on the same domain, by security reasons. 重要的是,出于安全原因,这仅在iframe和您的父窗口位于同一域中时有效。

This will do the trick - just keep in mind that different browsers will accept different maximum lengths of dataURL. 这将达到目的-请记住,不同的浏览器将接受不同的dataURL最大长度。

<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
function byId(e){return document.getElementById(e);}
function allByClass(className){return document.getElementsByClassName(className);}
function newEl(tag){return document.createElement(tag);}
function newTxt(txt){return document.createTextNode(txt);}

function toggleClass(elem, className){elem.classList.toggle(className);}
function toggleClassById(targetElemId, className){byId(targetElemId).classList.toggle(className)}

function hasClass(elem, className){return elem.classList.contains(className);}
function addClass(elem, className){return elem.classList.add(className);}
function removeClass(elem, className){return elem.classList.remove(className);}

function forEachNode(nodeList, func){for (var i=0, n=nodeList.length; i<n; i++) func(nodeList[i], i, nodeList); }

window.addEventListener('load', onDocLoaded, false);

function onDocLoaded()
{
    byId('displayBtn').addEventListener('click', onDisplayBtn, false);
}

function onDisplayBtn()
{
    var rawInput = byId('htmlInput').value;
    var base64Output = "data:text/html;base64," + btoa(rawInput);
    byId('htmlOutput').src = base64Output;
}
</script>
<style>
</style>
</head>
<body>
    <textarea id="htmlInput" style="width: 462px; height: 185px;"></textarea>
    <hr>
    <button id='displayBtn'>Display</button>
    <br>
    <iframe id='htmlOutput' style="width: 462px;"></iframe>
</body>
</html>

如何从 html 获取和发送值<textarea>&lt;i&gt;to a PHP page?&lt;/div&gt;&lt;/i&gt;&lt;b&gt;到 PHP 页面?&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我创建了一个用于图像上传的表单。 此表单还包括一个标签,用于添加有关正在上传的图像的信息。 每当单击“上传”按钮时,我都需要将标签中的文本发送到页面“savetofile.php”。</p><p> 目前图片上传正常,图片信息发送正确,但我无法从“savetofile.php”页面的标签中获取文本。</p><p> 形式</p><pre>&lt;form action="savetofile.php" enctype="multipart/form-data" id="form1" method="post"&gt; &lt;div&gt; &lt;label for="fileToUpload"&gt;Take or select photo(s)&lt;/label&gt;&lt;br/&gt; &lt;input accept="image/*" capture id="fileToUpload" name="fileToUpload" onchange="fileSelected();" type="file"/&gt; &lt;/div&gt; &lt;div&gt; &lt;br&gt;&lt;br&gt; &lt;label for="notes"&gt;Notes:&lt;/label&gt; &lt;textarea cols="50" id="notes" maxlength="2000" name="notes" placeholder="Please enter any additional information here" rows="4" onchange="this.form.submit()"&gt;&lt;/textarea&gt; &lt;br&gt;&lt;br&gt; &lt;/div&gt; &lt;div id="details"&gt;&lt;/div&gt; &lt;div&gt; &lt;input onclick="uploadFile()" type="button" value="Upload"/&gt; &lt;/div&gt; &lt;div id="progress"&gt;&lt;/div&gt; &lt;/form&gt;</pre><p> 保存到文件.php</p><pre> $text = $_POST['notes']; _log('text: '.$text);</pre><p> 上传文件()</p><pre> function uploadFile() { var fd = new FormData(); var count = document.getElementById('fileToUpload').files.length; for (var index = 0; index &lt; count; index++) { var file = document.getElementById('fileToUpload').files[index]; fd.append('myFile', file); } var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress, false); xhr.addEventListener("load", uploadComplete, false); xhr.addEventListener("error", uploadFailed, false); xhr.addEventListener("abort", uploadCanceled, false); xhr.open("POST", "savetofile.php"); xhr.send(fd); }</pre><p> 当前 output:</p><pre> Undefined index: notes... text:</pre></div> - How to get and send values from html <textarea> to a PHP page?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将正文从.js页发送到.php页 - how to send a body from a textarea from a .js page to a .php page 如何从 html 获取和发送值<textarea>&lt;i&gt;to a PHP page?&lt;/div&gt;&lt;/i&gt;&lt;b&gt;到 PHP 页面?&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我创建了一个用于图像上传的表单。 此表单还包括一个标签,用于添加有关正在上传的图像的信息。 每当单击“上传”按钮时,我都需要将标签中的文本发送到页面“savetofile.php”。</p><p> 目前图片上传正常,图片信息发送正确,但我无法从“savetofile.php”页面的标签中获取文本。</p><p> 形式</p><pre>&lt;form action="savetofile.php" enctype="multipart/form-data" id="form1" method="post"&gt; &lt;div&gt; &lt;label for="fileToUpload"&gt;Take or select photo(s)&lt;/label&gt;&lt;br/&gt; &lt;input accept="image/*" capture id="fileToUpload" name="fileToUpload" onchange="fileSelected();" type="file"/&gt; &lt;/div&gt; &lt;div&gt; &lt;br&gt;&lt;br&gt; &lt;label for="notes"&gt;Notes:&lt;/label&gt; &lt;textarea cols="50" id="notes" maxlength="2000" name="notes" placeholder="Please enter any additional information here" rows="4" onchange="this.form.submit()"&gt;&lt;/textarea&gt; &lt;br&gt;&lt;br&gt; &lt;/div&gt; &lt;div id="details"&gt;&lt;/div&gt; &lt;div&gt; &lt;input onclick="uploadFile()" type="button" value="Upload"/&gt; &lt;/div&gt; &lt;div id="progress"&gt;&lt;/div&gt; &lt;/form&gt;</pre><p> 保存到文件.php</p><pre> $text = $_POST['notes']; _log('text: '.$text);</pre><p> 上传文件()</p><pre> function uploadFile() { var fd = new FormData(); var count = document.getElementById('fileToUpload').files.length; for (var index = 0; index &lt; count; index++) { var file = document.getElementById('fileToUpload').files[index]; fd.append('myFile', file); } var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress, false); xhr.addEventListener("load", uploadComplete, false); xhr.addEventListener("error", uploadFailed, false); xhr.addEventListener("abort", uploadCanceled, false); xhr.open("POST", "savetofile.php"); xhr.send(fd); }</pre><p> 当前 output:</p><pre> Undefined index: notes... text:</pre></div> - How to get and send values from html <textarea> to a PHP page? 如何将div宽度从iframe发送到父页面 - How to send the div width from an iframe to the parent page 如何从父网页运行iFrame的脚本? - How to run an iFrame's script from the parent web page? 如何防止网页知道它已加载到iframe中? - How to prevent a web-page from knowing that it is loaded inside an iframe? 从主页上的iframe发送消息 - Send a message from an iframe on the main page 从iframe,如何发送XMLHttpRequest? - From an iframe, how to send an XMLHttpRequest? 从iframe到网页的按键事件无法在iframe中使用 - Keypress event to web page not working from iframe 如何从 php 中的 suneditor textarea 发送值? - How to send value from suneditor textarea in php? 如何将我的 web 页面插入到 iframe 中? - How to insert my web page into an iframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM