Using Relative URLs with window.open

I am using the JS code below to open a new window by populating dynamically generated code.

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; } 

The relative URLs used in this document say that for img src does not work in this document.

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

EDIT:

I dynamically populate the content using javascript, you just need to have a valid URL in the browser window for my links and links to the image source to work.

PS The page specified in the js code has no physical existence.

+4
source share
2 answers

how to assign a url to a newly opened window

You need to pass and the window.open() url

 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

+6
source

Usually you will open a window in which all parameters in the function will be indicated:

 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 :

 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
source

Source: https://habr.com/ru/post/1500890/


All Articles