Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12367

How to load result of a javascript function to a new tab

I've got a function that does Ajax to load certain html portion. Sometimes I need the result to be displayed in a separate window (or a tab if you will). When I include that function call in an anchor element's href attribute I can get that behavior:

   <a href="javascript:viewInternalDocument('AF7GH209')" target="_blank">Related document</>

But if I try to do it manually by calling window.open, I just get blank tab:

 window.open("javascript:viewInternalDocument('AF7GH209')","_blank");

In fact, the new tab opens even if I omit the _blank argument.

If you wonder what my viewInternalDocument function does, then here's the internals of it:

    function viewInternalDoc(docID) {
    var url = "/Cabinet/ViewInternalDocument";
    var args = { docID: docID};
    $.get(url, args, function (html) {
        $('#divContentHierarchy').html(html);
    }).fail(function () {
        alert('Error loading selected document')
    });
}

When I try window.open('javascript:alert("Hello")'), I can get the alert in a new tab (again, omitted the "_blank"). So what is the problem with not displaying the result of my function?

EDIT: I just realized that function does not work with the anchor element either. I somehow confused. Sorry for that.

Upvotes: 0

Views: 140

Answers (1)

Chris Malherbe
Chris Malherbe

Reputation: 238

You can create a new window, get your content and then insert content into the window with jQuery:

  var w = window.open();
  $(w.document.body).html(viewInternalDocument('AF7GH209'));

or with vanillaJS:

var w = window.open();
w.document.body.innerHTML = viewInternalDocument('AF7GH209');

Upvotes: 1

Related Questions