modifying a new window using Javascript contained in the parent page
Is there a way by which a user can click on a link in a webpage, which would then trigger a new window being opened up, which is then populated with content by javascript from the original page?
I need to write a s开发者_StackOverflowelf contained HTML file (so cannot use external links) that is able to BUILD a new window with predefined content...
Yes. JavaScript's window.open
method should be used for opening a new window.
That method returns back an object corresponding to a new window, so your JavaScript code can now access new window's DOM objects using that object.
See this.
You can open a new window (window.open
) and write the content of the inner document stream programmatically, using document.write
.
function example () {
var newWindow = window.open('about:blank','name','height=400,width=500');
newWindow.document.write('<html><head><title>Test</title>');
newWindow.document.write('</head><body>');
newWindow.document.write('<p>Test page generated programmatically.</p>');
newWindow.document.write('</body></html>');
newWindow.document.close();
}
Here is a basic example of writing content to a child window:
child_window = window.open('', 'name', 'width=300,height=300');
child_window.document.write('<h1>Hello World</h1>');
精彩评论