使用windows .open的相对url

时间:2022-10-22 15:25:49

I am using below JS code to open new window by populating dynamic generated code..

我正在使用下面的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;
}

Relative urls used in this document say for img src, in this document is not working.

本文档中使用的相对url表示img src无效。

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

EDIT:

编辑:

I populate the content dynamically using javascript, just need a valid url in the brower window, to make my hyperlinks & image source ref to work.

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

P.S. The page pointed out in the js code, doesn't have physical existance.

js代码中指出的页面没有物理存在。

2 个解决方案

#1


7  

how to assign the url to the newly opened window

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

You need to pass and URL to window.open()

您需要将URL传递到windows .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

Or,

或者,

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

Or, You can even say,

或者,你甚至可以说,

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

Refer Window.location

参考Window.location

#2


1  

Normally you would open a window giving all the parameters in the function like:

通常,你会打开一个窗口,给出函数中的所有参数,比如:

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

But you could do it like what you did but you used the wrong variable to add your url. It should be w.document.location.href:

但是你可以像这样做但是你用了错误的变量来添加你的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;

#1


7  

how to assign the url to the newly opened window

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

You need to pass and URL to window.open()

您需要将URL传递到windows .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

Or,

或者,

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

Or, You can even say,

或者,你甚至可以说,

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

Refer Window.location

参考Window.location

#2


1  

Normally you would open a window giving all the parameters in the function like:

通常,你会打开一个窗口,给出函数中的所有参数,比如:

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

But you could do it like what you did but you used the wrong variable to add your url. It should be w.document.location.href:

但是你可以像这样做但是你用了错误的变量来添加你的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;