简体   繁体   中英

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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