简体   繁体   中英

Opening a new browser window with specificed HTML

I have a webpage which dynamically creates markup within itself. On the click of a certain button I want a client-side event to 'copy' this markup and place it in the <body> of a blank page which it will open in a new browser window. Is there any way to do this?

As you want it on the client-side using JavaScript (from your tags), you can use the window.open .

var w = window.open("", "Some Title");
w.document.body.innerHTML = "yay!";

Note that, in this example, the target inside window.open function is a blank string which means that the new window opened will be a blank window (not redirected to any URL like your website's).

When opening a new window, the function returns a "handle" to this window.

You can then manipulate the new window with javascript (so long as it points to a URL on the same domain).

var newWindow = window.open ("", "mywindow", "location=1,status=1,scrollbars=1,width=100,height=100");
newWindow.document.write("Hello world");

The newWindow.document gives you access to the DOM of the new window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM