【问题标题】:Using relative urls with window.open在 window.open 中使用相对 url
【发布时间】:2013-09-06 20:28:27
【问题描述】:

我正在使用下面的 JS 代码通过填充动态生成的代码来打开新窗口..

function OpenWindow(obj) {
    var w = window.open();
    var stored = $(obj).parent().find("div").html();
    w.document.title = "New Window";
    w.document.URL = "hello.com/dummypage.html"; //how to assign the url to the newly opened window
    $(w.document.body).html(stored);
    return false;
}

本文档中使用的相对 URL 为 img src,在本文档中无效。

<tr><td colspan='2' align='center'><img id='imglegend' src='/images/Legend.jpg'></td></tr>

编辑:

我使用 javascript 动态填充内容,只需要在浏览器窗口中提供一个有效的 url,就可以使我的超链接和图像源引用工作。

附: js代码中指出的页面,并不存在。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    如何将url分配给新打开的窗口

    您需要将 URL 传递给window.open()

    window.open('http://www.google.com');//will open www.google.com in new window.
    window.open('/relative_url'); //opens relatively(relative to current URL) specified URL
    

    或者,

    function OpenWindow(obj) {
        var w = window.open();
        w.location = "hello.com/dummypage.html"; //how to assign the url to the newly opened window
    }
    

    或者, 你甚至可以说,

    w.location.assign("http://www.mozilla.org");
    

    参考Window.location

    【讨论】:

    • 谢谢,但在我的情况下,完整的网址是未知的,我可以提供相对路径吗?
    【解决方案2】:

    通常您会打开一个窗口,提供函数中的所有参数,例如:

    window.open('yoururl','title','some additional parameters');
    

    但是你可以像你所做的那样做,但是你使用了错误的变量来添加你的 url。应该是w.document.location.href:

    var w = window.open();
    w.document.title = "New window";
    w.document.location.href = "hello.com"; //how to assign the url to the newly opened window
    $(w.document.body).html(stored);
    return false;
    

    【讨论】:

    • w.document.location.href = "hello.com"; 会将打开的页面重定向到 hello.com。
    • 是的。这就是 OP 想要的。
    • @putvande 不,我不想那样做。我使用 javascript 动态填充内容,只需要在浏览器窗口中有一个有效的 url,就可以使我的超链接和图像源参考工作。
    • 没有 OP 想要打开具有指定 URL 的窗口。如果我做得对,你能检查我的更新代码吗?
    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多