Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using relative urls with window.open

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.

like image 815
Abhijeet Avatar asked Sep 06 '13 10:09

Abhijeet


People also ask

What problems can there be with use of relative URLs?

When you have a menu structure that relies on relative URLs, one wrong link in your content to your test environment would cause the entire test environment to be spidered and indexed, causing massive duplicate content issues.

Why is it always best to use relative URLs?

The relative format allows the same website to exist on staging and production domain, or the live accessible version of your website, without having to go back in and re-code all of the URLs. This not only makes coding easier for a web developer but also serves as a time saver.

Should I use absolute or relative URLs?

If you do a lot of testing and move content frequently between domains, then relative links are the best solution. Absolute paths are more secure, protect content, and prevent duplicate pages. The main point is that the link format you prefer should be applied to all URLs on the site.


2 Answers

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

like image 126
Deepak Ingole Avatar answered Sep 21 '22 13:09

Deepak Ingole


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;
like image 34
putvande Avatar answered Sep 20 '22 13:09

putvande