Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use JavaScript to send HTML to a new tab/window and have the CSS loaded?

I have some JavaScript in a page that makes a call to a server and gets some HTML that I want to display in a new browser tab or window. I can use window.document.write(myHTML) to put the HTML in the new container but the HTML contains some CSS and JavaScript includes and these do not get included by the new window. Is there any way to get the browser to take the HTML and fully evaluate it?

Thanks

Paul

like image 950
Paul Avatar asked Oct 21 '25 05:10

Paul


2 Answers

Can you post your example javascript and the HTML you're writing to the window (or a stripped down test case, really)? If you're writing out a full HTML document (including html/head/body elements) using newWindow.document.write, it should work. Does your javascript look something like this?

var newWindow = window.open();
newWindow.document.write("<html><head><script type='text/javascript' src='yourCode.js'></scr"+"ipt></head><body>Your content</body></html>");
newWindow.document.close();
like image 95
Jennifer Grucza Avatar answered Oct 23 '25 21:10

Jennifer Grucza


this worked for me.. the code doesn't seem to work in the editor here but you can try it in your browser console..

var newTab = window;
var pageContent = "<head> your html head should be here </head>";
    pageContent += "<body> the body should be <a class='heads'>here</a> </body>";
    newTab.open().document.write(pageContent);
.heads{
    font-size:50px;
    color: green;
  }
like image 37
Khobby Kurtiz Avatar answered Oct 23 '25 19:10

Khobby Kurtiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!