jQuery 在 window.open 中使用相对 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18655201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:08:59  来源:igfitidea点击:

Using relative urls with window.open

javascriptjqueryhtml

提问by Abhijeet

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 表示 for 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 动态填充内容,只需要浏览器窗口中的有效 url,即可使我的超链接和图像源引用正常工作。

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

PS js代码中指出的页面,没有实体存在。

回答by Dipak Ingole

how to assign the url to the newly opened window

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

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

您需要将 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

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

回答by putvande

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;