Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What does the open() method mean in this code

    I went to the following site, and well, i didnt get a great deal of it:

    Mozilla

    The example they gave, goes as follows:

    Code:
    <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,

    Code:
    <SCRIPT LANGUAGE="JScript">
    function foo() {
        open('about:blank');}
    </SCRIPT>
    <BODY onclick="foo();">
    Click this page and window.open() is called.
    </BODY>
    And,
    Code:
    <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?
    LovesWar

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SpiritualStorms
    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.

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    So does the example mean i can insert a new document into my existing

    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:
    Code:
    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,
    Code:
    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.
    Last edited by SpiritualStorms; 06-26-2004 at 12:48 AM.
    LovesWar

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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([url], [name], [features], [replace])

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

    http://msdn.microsoft.com/library/de...ods/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/.../doc_open.html

    DevGuru is also a good site for other info

    aDog
    Moderator at WebExpertz Forums

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to clarify their example, if you run the following code:

    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
    Moderator at WebExpertz Forums

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    So does the example mean i can insert a new document into my existing

    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.
    LovesWar

  • #7
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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() to .. well.. create a new element.. and then add it to the document using appendChild().
    Last edited by shlagish; 06-26-2004 at 05:42 AM.
    Shawn


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •