简体   繁体   English

如何将javascript函数的结果加载到新选项卡

[英]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. 我有一个函数可以执行Ajax来加载某些html部分。 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: 当我在锚元素的href属性中包含该函数调用时,我可以得到该行为:

   <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手动进行操作,则会得到空白标签:

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

In fact, the new tab opens even if I omit the _blank argument. 实际上,即使我省略了_blank参数,新选项卡也会打开。

If you wonder what my viewInternalDocument function does, then here's the internals of it: 如果您想知道我的viewInternalDocument函数做什么,那么这是它的内部结构:

    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"). 当我尝试window.open('javascript:alert("Hello")') ,我可以在新选项卡中获得警报(同样,省略了“ _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. 抱歉

You can create a new window, get your content and then insert content into the window with jQuery: 您可以创建一个新窗口,获取您的内容,然后使用jQuery将内容插入该窗口中:

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

or with vanillaJS: 或使用vanillaJS:

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

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

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