简体   繁体   English

使用JavaScript函数的结果打开新选项卡

[英]Open New Tab with Results from JavaScript function

This doesn't work: 这不起作用:

<a href='javascript:void(0)' target='_blank' onclick='do_stuff()' >Open</a>

And neither does this 这也不是

<a href='javascript:void(0)' target='_blank'onclick='window.open(do_stuff(), "_blank");' >View</a>

It might be clear what I am trying to do. 可能很清楚我想要做什么。 I need to open up a new tab with the results of a JavaScript function. 我需要打开一个包含JavaScript函数结果的新选项卡。 Is there another way I can try to do this? 还有其他方法可以尝试这样做吗?

Open new window (tab) and save it to a variable, then change it's content to your function output: 打开新窗口(选项卡)并将其保存到变量,然后将其内容更改为您的函数输出:

<button onclick="clicked()">Test</button>

<script>

var clicked = function () {
  var new_page = window.open();
  new_page.document.write("output");
}

</script>

You can use JS to create divs and change it's content: 您可以使用JS创建div并更改其内容:

new_page.document.getElementById('test').innerHTML = "test";

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM