11

I am using below JS code to open new window by populating dynamic 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;
}

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

<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.

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

2 Answers 2

18

how to assign the url to the newly opened window

You need to pass and URL to 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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but in my case full url is not known, can I give relative path?
@Abhijeet the answer is yes. Passing /dummypage.html to window.open() will open relative to the current URL.
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:

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;

7 Comments

w.document.location.href = "hello.com"; will redirect that opened page to hello.com.
Yes. And that is what OP wants.
@putvande No I do not want that. I populate the content dynamically using javascript, just need a valid url in the brower window, to make my hyperlinks & image source ref work.
No OP wants to open a window with specified URL..Can you check my update code if I am doing right?
@Abhijeet I figured it out - there is apparently an HTML tag called <base> which you can use to set the hostname when using relative URLs. w3schools.com/tags/tag_base.asp
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.