3

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?

9
  • 1
    What results are you trying to pass to new window? Commented Oct 13, 2016 at 21:19
  • @guest271314 just HTML. Commented Oct 13, 2016 at 21:22
  • Can you show your code for do_stuff()? Commented Oct 13, 2016 at 21:22
  • do_stuff() calls ajax and returns HTML. Not much more than that. the return function populates something like $("#myDiv").html(data); Commented Oct 13, 2016 at 21:25
  • Have you checked this answer for HTML content using window.open. Might help Commented Oct 13, 2016 at 21:27

1 Answer 1

5

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";
Sign up to request clarification or add additional context in comments.

3 Comments

I really like that jQuery option. Let me try and get back.
sorry, changed my code to JS and it should work now, will try to come up with a version for jQuery
Thanks, this code works so I will mark as correct. However, as I explained above just now, I ended up not needing to dot his with my project as I misread my code a bit. Thanks for your help! I may use this in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.