如何将textarea的内容发送到新窗口

时间:2015-01-31 09:26:35

标签: javascript html textarea

我想知道如何使用Window open()方法将textarea的内容发送到新窗口。

继承人是我的小代码:

HTML

 <textarea name="textcode"></textarea>
 <button  type="submit" onclick="myFunction()">Open in New window</button>

的Javascript

function myFunction() {
    window.open("insert the new window path here");
}

2 个答案:

答案 0 :(得分:0)

使用jquery在代码下面使用。

HTML

<textarea name="textcode" id="text"></textarea>
<button  type="button" id="openwindow">Open in New window</button>

jquery

$(document).ready(function(){
    $('#openwindow').click(function(){
      var value  = $('#text').val();
      window.open('https://www.google.co.in/?#q='+value);
    });
});

你可以将值传递为params.include路径和参数。你将获得查询字符串中文本区域的值。使用PHP $ _GET ['param']你将在新窗口中打印数据。

使用纯javascript HTML

<textarea name="textcode" id="text"></textarea>
<button  type="button" onclick="myFunction();">Open in New window</button>

的javascript

 function myFunction(){
    var value  = document.getElementById("text").value;
    window.open('https://www.google.co.in/?#q='+value);
 }

答案 1 :(得分:0)

如果您可以直接访问父窗口的内容/ DOM,为什么尝试在Windows之间发送数据?当您的textarea的内容可以/可以更改时,这将为您提供优势。 “子页面”将保留对打开它的页面的引用!

parent.html

<textarea id="mytext" name="textcode"></textarea>
<button  type="submit" onclick="myFunction()">Open in New window</button>
<script>
function myFunction() {
    //open popup/window on the same domain
    window.open('newwindow.html','newwindow','width=350,height=350');
}
</script>

当新窗口打开时,只有当它们位于同一个域时,才能访问它的父DOM。

newwindow.html中,您可以使用window.opener访问textarea(或任何其他DOM元素):

window.opener.document.getElementById("mytext").value

就像我说的,这只适用于您的父窗口和子窗口位于同一个域中的情况。当父母和孩子不在同一个域中时,您将遇到“same-origin policy errors”。

然而!您可以使用Window.postMessage在不在同一域的窗口之间安全地传递数据。在所有浏览器中都不支持此功能。

otherWindow.postMessage(message, targetOrigin, [transfer]);

您可以在mozilla here

的开发页面上找到示例和其他信息(如浏览器支持等)
相关问题