将相对网址与 window.open 一起使用

Using relative urls with window.open

本文关键字:open 一起 window 相对      更新时间:2023-09-26

我正在使用下面的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代码中指出的页面没有物理存在。

如何将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.open('yoururl','title','some additional parameters');

但是你可以像你所做的那样做,但你使用了错误的变量来添加你的网址。应该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;