Jump to content

window.open questions


shadowayex

Recommended Posts

I'm trying to build a javascript application to help me teach a class. I have one working version, but I'm trying to compact code a little bit, so I came up with an idea. I built a small structured model that hasn't worked. Basically I'm trying to have one window open another window, and have the first window edit the second window's content via document.getElementById. I know i can edit via document.write, and I'm using that for my first version, but it's a lot of code. Here's an example:I can:

var openit = window.open("page.html", "", "*features here*");openit.document.write("hi");

And the window will appear with hi in it.But I would like to make it so text will go into certain tags. Something like:Assume the page has a div with the id "main" on it:

var openit = window.open("page.html", "", "*features here*");openit.document.getElementById("main").innerHTML = "hi";

In theory, it should work the same as the first, but does not.Is this because getElementById is not supported in this situation as write is? And if that's the case, is there a different function I can use to obtain these results?EDIT: If someone would like to see the code of my first one, let me know and I'll gladly post it. It's a page builder that I can show the class example on without having to save any files, but once I update it, I'll probably make an open to save the page.

Link to comment
Share on other sites

The problem is you are trying to access the element with ID "main" before it loads - it is simply not there yet. You could try putting the assignment inside the onload handler for the window.

var openit = window.open("page.html", "", "*features here*");openit.onload = function() {	openit.document.getElementById("main").innerHTML = "hi";}

Link to comment
Share on other sites

Well, the concept works, but is there any way I can make it automatically right stuff into a <script> or <style> tag. I used openit.body.innerHTML to edit the body and it worked fine. Then I tried:openit.document.getElementsByTagName("script").innerHTML = "alert(\"hi\")";openit.document.getElementsByTagName("style").innerHTML = "body{background-color: #330099;}";but those didn't work >_<. Is there a way to input text into those as well?

Link to comment
Share on other sites

Couple things.1. getElementsByTagName returns a collection, not an element, even if there is only one member in the collection. So you have to access members by [index]. If there is just one of each, then of course the index will be [0].2. I'm not sure if you're still working with an empty document or if you're loading page.html. So: if you want to edit the script and style, make sure those tags exist first.

Link to comment
Share on other sites

Ok so after googling, here's what I got:

			function openPage() {				var features="status=no,scrollbars=yes,resizable=yes,menubar=yes,width=2000,height=2000,top=0,left=0";				var openit = window.open("", "HelpWindow", features);				openit.document.getElementsByTagName("title")[0].innerHTML = "Testing";		openit.document.getElementsByTagName("style")[0].innerHTML = "body{background-color:#0066ff;}";		openit.document.getElementsByTagName("script")[0].innerHTML = "alert(\"Hi!\");";		openit.document.body.innerHTML = "<p>Hello<br />Everyone!</p>";			}

Doesn't work. When I take the [0] stuff out, the last line does, but the first few don't. Am I getting the wrong idea or?

Link to comment
Share on other sites

Ah ha! Don't know why I took that out. Well it's back in and looks like:

			function openPage() {				var features="status=no,scrollbars=yes,resizable=yes,menubar=yes,width=2000,height=2000,top=0,left=0";				var openit = window.open("", "HelpWindow", features);		openit.onload = function() {					openit.document.getElementsByTagName("title").innerHTML[0] = "Testing";			openit.document.getElementsByTagName("style").innerHTML[0] = "body{background-color:#0066ff;}";			openit.document.getElementsByTagName("script").innerHTML[0] = "alert(\"Hi!\");";			openit.document.body.innerHTML = "<p>Hello<br />Everyone!</p>";		}			}

And for now, it servers it's purpose. Now I just gotta edit it to what it's really supposed to do. If anyone would like to see it when it's done, send me a PM asking and I'll send the code to and you can try it. It's a JS page generator to make pages, preview them, preview the actual source code, and save the actual file.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...