...

View Full Version : What does the open() method mean in this code



SpiritualStorms
06-24-2004, 07:26 AM
I went to the following site, and well, i didnt get a great deal of it:

Mozilla (http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/open_1.asp)

The example they gave, goes as follows:


<HTML>
<HEAD>
<TITLE>First Document</TITLE>
<script>
function replace(){
var oNewDoc = document.open("text/html", "replace");
var sMarkup = "<HTML><HEAD><TITLE>New Document</TITLE><BODY>Hello, world</BODY></HTML>";
oNewDoc.write(sMarkup);
oNewDoc.close();
}
</script>
</HEAD>
<BODY>
<h1>I just want to say</h1><br>
<!--Button will call the replace function and replace the current page with a new one-->
<Input type ="button" value = "Finish Sentence" onclick="replace();">
</BODY>
</html>



The other 2 examples they gave, went like this,


<SCRIPT LANGUAGE="JScript">
function foo() {
open('about:blank');}
</SCRIPT>
<BODY onclick="foo();">
Click this page and window.open() is called.
</BODY>


And,

<BUTTON onclick="open('Sample.htm');">
Click this button and document.open() is called.
</BUTTON>


Are they saying that i can basically load any html file into a window that is already open, without the window reloading? Or are they saying something else?

Alot of what was said, just went over my head. A large part was because of how they structured the syntax. If you go to the link i gave, you will see that the opening, goes as follows:


oNewDoc = document.open(sUrl [, sName] [, sFeatures] [, bReplace])


I did not understand the above generalisation. I think what is throwing me off is the straight brackets within the open brackets.

At any rate, i was wondering if one would be willing to go through the process of breaking the above down for me?

neofibril
06-25-2004, 09:55 AM
Alot of what was said, just went over my head. A large part was because of how they structured the syntax. If you go to the link i gave, you will see that the opening, goes as follows:

oNewDoc = document.open(sUrl [, sName] [, sFeatures] [, bReplace])

I did not understand the above generalisation. I think what is throwing me off is the straight brackets within the open brackets.

At any rate, i was wondering if one would be willing to go through the process of breaking the above down for me?

Brackets indicate optional parameters.

All parameters are described in the table immediately below the example syntax.

If you'd like to know more about the effects of the write() method, go to the reference page for that.

SpiritualStorms
06-26-2004, 12:45 AM
window?

I still dont get what the value is of using the document.write() method when it seems like the code might just as well be written by hand at the time of code. It just seems like unless you do a lot of for loops, the document.write() method is pretty usueless. It is simply additional typing to write something as cheesy as the following:


document.write("<html>");
document.write("<head> <title> Something </title> </head>");
document.write("<body bgcolor='red' background='imageWhatever.gif' >");
document.write("<br>");
document.write("<br>");
document.write("<h2> A Title Goes Here!</h2>");
document.write("<br>");
document.write("<br>");
document.write("</body>");
document.write("</html>");



All of the above just seems like a waste of time since you could do without the document.write() method altogather.

My undertanding was that if you had a document already, you could add content to it with the document.write() method. This however is not turning out to be the case, so my main question is, how do you use this method in addition to the open(), and close() methods?

So far, from what i am told, the innerHTML property is about the only real method, or property that allows one to alter a document after it had beeen loaded, and not prior. Secondly, i thought, the open() method was a method of the window object, and not the document object? I mean, i have seen this,

function Launcher(paramOne, paramTwo, paramThree)
{
window.open(paramOne, paramTwo, paramThree);
}

so in this sense, i know that i am opening up a mini window. However, in the context of the quoted website, the suggestion is that i can apply the open() method to a webpage itself, and somehow edit the page itself through the open(), and write() methods. I am having a hard time with the over-all wording of their explanations.

Arielladog
06-26-2004, 02:13 AM
I'm not 100% what's going on, but my take is below:

There are two separate functions: window.open and document.open

The first is, of course, to open a new URL.

It should be like:

window.open(, [name], [features], [replace])

All of those parameters are actually optional, so they deserve the brackets :D (but brackets are usually done differently that I've done above)

[url]http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/open_0.asp -- I'm not really sure about replace as a last argument

There is another function called document.open

document.open( [mimeType], [replace] )

With one two optional parameters (first one is like "text/html", etc...)

For internet explorer, if you pass document.open() more than 2 arguments, it seems to actually use window.open().

document.open() is used to open up the stream to the browser (so document.write and document.writeln can do their stuff). window.open() opens up a new browser window.

For info on document.open() and stuff, you can look here:

http://www.devguru.com/Technologies/ecmascript/quickref/doc_open.html

DevGuru is also a good site for other info

aDog :cool:

Arielladog
06-26-2004, 02:21 AM
Just to clarify their example, if you run the following code:



<html>
<head>
<SCRIPT>
function foo() {
alert(this); // returns window
}
</SCRIPT>
</head>
<BODY onclick="foo();">
When a function fired by an event on any object calls the open method, the window.open method is implied.

Click this page and window.open() is called.
When an event on any object calls the open method, the document.open method is implied.

<BUTTON onclick="alert(this)">
Click this button and document.open() is called.
</BUTTON>
</BODY>
</html>


You'll see what they me. If it's run under Mozilla you can actually see what the object is rather than just "[Object object]"

Anyway, in the foo(), this refers to the window object, so when open() is called, it calls window.open()

For the second case, this refers to the button elements, so it doesn't have the scope the first one does. I'm not 100% sure why, but in this case, it's scope goes to the document before the window, and thus the document.open() method is called and not the window.open() one.

aDog :cool:

SpiritualStorms
06-26-2004, 05:35 AM
into the document itself?

Again, my understanding is, that window generally refers to the actual browser window that will display your webpages, whereas, document, refers literally to the webpage that you want to display. They are not one in the same. In an analogy, you could say that the relationship between a window, and a document, is alot like a painting, and a frame. One is the host of the other, and not the other way around. Window then, refers to the frame under which your files will be shown through.

This being the case, i want some additional explanations on your following:


document.open() is used to open up the stream to the browser (so document.write and document.writeln can do their stuff). window.open() opens up a new browser window.

When you say it opens up a stream, do you mean like a channel, or a passageway into the document? And if a passage way is opened up, does it mean i can send content to the document through, or via, the write() method? Thirdly, do i use the write() method, before, or after the page has loaded?

I wonder if anyone knows what i am trying to say.

shlagish
06-26-2004, 05:39 AM
Just to answer your other question..
for(var i=0;i<1001;++i){
document.write(i+"<br />");
}
This writes all the numbers from 0 to 1000.
Great time-saver compared to writting it out completely..
As you said, in for loops, the write() function becomes very handy.
As for modifying the page after it is loaded, you can also use createElement() (http://www.mozilla.org/docs/dom/domref/dom_doc_ref46.html) to .. well.. create a new element.. and then add it to the document using appendChild() (http://www.mozilla.org/docs/dom/domref/dom_el_ref32.html).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum