Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a new window and insert html into it using jQuery?

I am trying to open a new window from javascript but nothing is being inserted into the html:

var callScriptText = $('#callScriptText').html(); var url = '/Action/CallScript/?callScript=';  // Open the current call script in a new window var openWindow = window.open(url, 'callScriptPopup', 'width = 500, height = 500'); $(openWindow).html(callScriptText); 

Does anyone know why?

like image 392
FairyQueen Avatar asked Feb 22 '12 16:02

FairyQueen


People also ask

How do I open a new window in HTML?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.

HOW include HTML in jQuery?

To load external HTML into a <div>, wrap your code inside the load() function. To load a page in div in jQuery, use the load() method. Firstly, add the web page you want to add.


2 Answers

Here's an example to open a new window with content using jQuery

<script> function nWin() {   var w = window.open();   var html = $("#toNewWindow").html();      $(w.document.body).html(html); }  $(function() {     $("a#print").click(nWin); });​ </script>  <div id="toNewWindow">     <p>Your content here</p> </div>  <a href="javascript:;" id="print">Open</a>​ 

EDIT: For those who say that this code doesn't work, here's a jsfiddle to try it http://jsfiddle.net/8dXvt/

like image 161
Juanma Avatar answered Sep 28 '22 01:09

Juanma


Try this:

var x=window.open(); x.document.open(); x.document.write('content'); x.document.close(); 

I find it works in Chrome and IE.

like image 30
Emre Avatar answered Sep 28 '22 03:09

Emre