9

我正在使用下面的 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 表示 for img src, 在本文档中不起作用。

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

编辑:

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

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

4

2 回答 2

16

如何将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

于 2013-09-06T10:14:52.813 回答
1

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

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;
于 2013-09-06T10:26:39.287 回答