Welcome to WebmasterWorld Guest from 54.161.100.24

Forum Moderators: open

Message Too Old, No Replies

Open new page with content from a div.

     
10:09 am on Apr 4, 2004 (gmt 0)

New User

10+ Year Member

joined:Apr 4, 2004
posts:23
votes: 0


Hi all.
In my site each page has a div with id="main_content". It is useful to identify the main content, without considering menus, navigation bars and so on.
I need to open a new page containing the content of that div.

I tried:
<a href="javascript:document.getElementById('main_content').innerHTML;">Open</a>

Following this way it's possible to obtain the content in the div, but i can't figure out how to open a new window filled with it.

What's the solution?

TIA,
Paolo

12:58 pm on Apr 4, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 14, 2004
posts:1181
votes: 0


Write a function that stores the value of the HTML in a variable.

When you open your window, give it a name, as in:
myWin=window.open("","myWin","menubar,scrollbars,left=30px,top=40px,height=400px,width=600px");

Then write the variable to the window, as in (using myVar as the variable name):
myWin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">'+
'<html><head><title>My Window</title></head><body>' + myVar + '</body></html>')

9:34 pm on Apr 4, 2004 (gmt 0)

New User

10+ Year Member

joined:Apr 4, 2004
posts:23
votes: 0


Works great, thanks a lot!

Paolo

9:16 am on Apr 6, 2004 (gmt 0)

New User

10+ Year Member

joined:Apr 4, 2004
posts:23
votes: 0


I solved the previous problem with these simple lines of code:
var content=document.getElementById("content").innerHTML;
myWin=window.open('','myWin','menubar,scrollbars,left=30px,top=40px,height=400px,width=600px');
myWin.document.write(content);

But I'm stumped again :(
Now I need that the content will be displayed into a div (with id='dest_div') of the new window (called main_content).

I tried to execute the following code.. without success:

var newWin=window.open('main_content','newWin','menubar,scrollbars,left=30px,top=40px,height=400px,width=600px');
var orig_div=document.createTextNode(document.getElementById("content").innerHTML);
var dest_div=newWin.document.getElementById("dest_div");
dest_div.appendChild(orig_div);

Would you help? Basic tutorials about dom are also welcome!

Thanks again,
Paolo

3:29 pm on Apr 6, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 14, 2004
posts:1181
votes: 0


I'll try to give it a look this evening; have to go to work now. One thing, though I doubt it will cure the problem, I think it should be:
newWin.document.dest_div.appendChild(orig_div);
2:05 am on Apr 7, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 14, 2004
posts:1181
votes: 0


See if this is what you are after:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Writing text to window 04.06.04</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
<!--
function doWin(){
var orig_div_cont=document.getElementById("content").innerHTML;
var myWin=window.open("","myWin","menubar,scrollbars,left=30px,top=40px,height=400px,width=600px");
myWin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">'+
'<html><head><title>My Window</title></head><body><div id="dest_div">'+
'Some original division content. <br /></div></body></html>');
myWin.document.close();
myWin.document.getElementById("dest_div").innerHTML+=orig_div_cont;
}
//-->
</script>
</head>
<body onload="doWin();">
<div id="content">Content from the first page's division. </div>
</body>
</html>

My favorite resources for programming DHTML are the O'Reilly books, "JavaScript: The Definitive Guide" by David Flanagan, ISBN 0-596-00048-0 and "Dynamic HTML" by Danny Goodman, ISBN 0-596-00316-1.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members