1

I am trying to open some content in new tab or new window. The code which I am trying is

var w = window.open();

$(w.document.head).html("<link rel='stylesheet' href='style.css'/>");

$(w.document.body).html(data);

where data represents the html contents that is to be placed on the new window.The problem is that when opening new window all content is appearing, but its styles are missing. While checking tag in head shows undefined. How can I display the data in new window with proper styles?

Peter Campbell
  • 661
  • 1
  • 7
  • 35
Adhi
  • 35
  • 1
  • 7

4 Answers4

2

Replace the *** with your html code

var w = window.open();
w.document.write('<html><head><title>Test</title>');
w.document.write('<link rel="stylesheet" href="style.css">');
w.document.write('</head><body>');
w.document.write('<p>This is the new page.</p>');
w.document.write('</body></html>');
w.document.close();

Let me know if this is useful :)

Admin Alex
  • 187
  • 10
-1

If you're using html you can just set the target to blank which will open the link in a new window:

<a href="http://www.example.com" target="_blank">This will open in a new window</a>

Hope this helps

Admin Alex
  • 187
  • 10
  • that's not possible in this case because the data that is to be loaded in new page has to be taken from backend.It's not a simple link as u specified – Adhi Jun 04 '15 at 10:29
-1

Unless the content of the new window must be created by the first window, on the client side, it is easier to use a page that is created by the server and named as a parameter under window.open() - like so:

window.open ('/popup-content.html');

https://developer.mozilla.org/en-US/docs/Web/API/Window/open

user2182349
  • 9,569
  • 3
  • 29
  • 41
-1

The following stackoverflow answer Add css file with jquery describes how to add an external file using append to the head element.

So in your case, something like

$(w.document.head).append("<link rel='stylesheet' href='style.css' type='text/css'/>");

should work

Community
  • 1
  • 1
Peter Campbell
  • 661
  • 1
  • 7
  • 35