views:

41

answers:

3

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 self contained HTML file (so cannot use external links) that is able to BUILD a new window with predefined content...

+2  A: 

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.

DVK
+1  A: 

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();
}
CMS
A: 

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>');
Trevor