create a web page with javascript on fly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • datactrl

    #1

    create a web page with javascript on fly

    Hi,

    Is that posible to create a web page completely with javascript and open it
    without request to server? Please show a simple sample. Thanks in advance!

    Jack


  • McKirahan

    #2
    Re: create a web page with javascript on fly

    "datactrl" <quals@tpg.com. au> wrote in message
    news:4165fd00$1 @dnews.tpgi.com .au...[color=blue]
    > Hi,
    >
    > Is that posible to create a web page completely with javascript and open[/color]
    it[color=blue]
    > without request to server? Please show a simple sample. Thanks in advance!
    >
    > Jack
    >[/color]

    A Web page is delivered by a Web server....

    Of course you can create a file with an .htm (or .html) extension and
    double-click on it in Windows Explorer and it will open. No JavaScript is
    necessary to do this; for example:

    <html>
    <head>
    <title>test1.ht m</title>
    </head>
    <body>
    <h1>Hello World</h1>
    </body>
    </html>

    You can create a page using JavaScript:

    <html>
    <head>
    <title>test2.ht m<title>
    <script type="text/javascript">
    var htm = "<html>";
    htm += "<head>";
    htm += "<title>test3.h tm;
    htm += "</head>";
    htm += "<body>";
    htm += "<h1>Hello World</h1>";
    htm += "</body>";
    htm += "</html>";
    document.write( htm);
    </script>
    </head>
    <body>
    <h1>Hello World</h1>
    </body>
    </html>

    Does this answer your question? If not please clarify.


    Comment

    • McKirahan

      #3
      Re: create a web page with javascript on fly

      "McKirahan" <News@McKirahan .com> wrote in message
      news:Con9d.3528 61$Fg5.74302@at tbi_s53...[color=blue]
      > "datactrl" <quals@tpg.com. au> wrote in message
      > news:4165fd00$1 @dnews.tpgi.com .au...[color=green]
      > > Hi,
      > >
      > > Is that posible to create a web page completely with javascript and open[/color]
      > it[color=green]
      > > without request to server? Please show a simple sample. Thanks in[/color][/color]
      advance![color=blue][color=green]
      > >
      > > Jack
      > >[/color]
      >
      > A Web page is delivered by a Web server....
      >
      > Of course you can create a file with an .htm (or .html) extension and
      > double-click on it in Windows Explorer and it will open. No JavaScript is
      > necessary to do this; for example:
      >
      > <html>
      > <head>
      > <title>test1.ht m</title>
      > </head>
      > <body>
      > <h1>Hello World</h1>
      > </body>
      > </html>
      >
      > You can create a page using JavaScript:
      >
      > <html>
      > <head>
      > <title>test2.ht m<title>
      > <script type="text/javascript">
      > var htm = "<html>";
      > htm += "<head>";
      > htm += "<title>test3.h tm;
      > htm += "</head>";
      > htm += "<body>";
      > htm += "<h1>Hello World</h1>";
      > htm += "</body>";
      > htm += "</html>";
      > document.write( htm);
      > </script>
      > </head>
      > <body>
      > <h1>Hello World</h1>
      > </body>
      > </html>
      >
      > Does this answer your question? If not please clarify.
      >[/color]

      Of course
      htm += "<title>test3.h tm;
      should have been
      htm += "<title>test3.h tm</title>";


      Here's another possibility.

      var htm = "<html>";
      htm += "<head>";
      htm += "<title>test4.h tm</title>";
      htm += "</head>";
      htm += "<body>";
      htm += "<h1>Hello World</h1>";
      htm += "</body>";
      htm += "</html>";
      var oIE = new ActiveXObject(" InternetExplore r.Application") ;
      oIE.visible = true;
      oIE.navigate("a bout:blank");
      oIE.document.op en;
      oIE.document.wr ite(htm);
      oIE.document.cl ose;


      Comment

      • Lee

        #4
        Re: create a web page with javascript on fly

        datactrl said:[color=blue]
        >
        >Hi,
        >
        >Is that posible to create a web page completely with javascript and open it
        >without request to server? Please show a simple sample. Thanks in advance![/color]

        Sure it is, but it has to be loaded from someplace, and it's also very
        simple to store a complete web page on the local disk. What, exactly,
        are you looking for?

        Comment

        • datactrl

          #5
          Re: create a web page with javascript on fly

          Thanks a lot, McKirahan. The last one is what I want. I would like to pop up
          a web page which is completely created by the original page with javascript.
          How about if I want to use showModelessDia log() to open it, is that
          possible? Thanks again !

          Jack

          "McKirahan" <News@McKirahan .com> wrote in message
          news:BAn9d.2091 30$D%.75621@att bi_s51...[color=blue]
          > "McKirahan" <News@McKirahan .com> wrote in message
          > news:Con9d.3528 61$Fg5.74302@at tbi_s53...[color=green]
          > > "datactrl" <quals@tpg.com. au> wrote in message
          > > news:4165fd00$1 @dnews.tpgi.com .au...[color=darkred]
          > > > Hi,
          > > >
          > > > Is that posible to create a web page completely with javascript and[/color][/color][/color]
          open[color=blue][color=green]
          > > it[color=darkred]
          > > > without request to server? Please show a simple sample. Thanks in[/color][/color]
          > advance![color=green][color=darkred]
          > > >
          > > > Jack
          > > >[/color]
          > >
          > > A Web page is delivered by a Web server....
          > >
          > > Of course you can create a file with an .htm (or .html) extension and
          > > double-click on it in Windows Explorer and it will open. No JavaScript[/color][/color]
          is[color=blue][color=green]
          > > necessary to do this; for example:
          > >
          > > <html>
          > > <head>
          > > <title>test1.ht m</title>
          > > </head>
          > > <body>
          > > <h1>Hello World</h1>
          > > </body>
          > > </html>
          > >
          > > You can create a page using JavaScript:
          > >
          > > <html>
          > > <head>
          > > <title>test2.ht m<title>
          > > <script type="text/javascript">
          > > var htm = "<html>";
          > > htm += "<head>";
          > > htm += "<title>test3.h tm;
          > > htm += "</head>";
          > > htm += "<body>";
          > > htm += "<h1>Hello World</h1>";
          > > htm += "</body>";
          > > htm += "</html>";
          > > document.write( htm);
          > > </script>
          > > </head>
          > > <body>
          > > <h1>Hello World</h1>
          > > </body>
          > > </html>
          > >
          > > Does this answer your question? If not please clarify.
          > >[/color]
          >
          > Of course
          > htm += "<title>test3.h tm;
          > should have been
          > htm += "<title>test3.h tm</title>";
          >
          >
          > Here's another possibility.
          >
          > var htm = "<html>";
          > htm += "<head>";
          > htm += "<title>test4.h tm</title>";
          > htm += "</head>";
          > htm += "<body>";
          > htm += "<h1>Hello World</h1>";
          > htm += "</body>";
          > htm += "</html>";
          > var oIE = new ActiveXObject(" InternetExplore r.Application") ;
          > oIE.visible = true;
          > oIE.navigate("a bout:blank");
          > oIE.document.op en;
          > oIE.document.wr ite(htm);
          > oIE.document.cl ose;
          >
          >[/color]


          Comment

          • RobG

            #6
            Re: create a web page with javascript on fly

            Have a search for a message subject "writing slabs of htm".
            It was conclusively shown by Lasse that it is much quicker
            to write HTML as elements in array, then use join("") to
            concatenate them before writing to the page. The length of
            time taken using += verus join() is exponentially longer,
            getting seriously long for big pages.

            There is an example of how to use it below.

            Of course, if your content is only small...

            Cheers, Rob.

            <html>
            <head>
            <title>Slabs of HTML</title>
            </head>
            <body>
            <script type="text/javascript">
            var a = [];
            var i = 0;
            a[0] = " <p>here is paragraph " + i + "</p>";
            a[++i] = " <p>here is paragraph " + i + "</p>";
            a[++i] = " <p>here is paragraph " + i + "</p>";
            a[++i] = " <table border='1' cellpadding='5' cellspacing='10 '>";
            a[++i] = " <tr>";
            a[++i] = " <td>This is a cell</td>";
            a[++i] = " <td>another cell</td>";
            a[++i] = " </tr>";
            a[++i] = " <tr>";
            a[++i] = " <td>This is a cell</td>";
            a[++i] = " <td>another cell</td>";
            a[++i] = " </tr>";
            a[++i] = " <tr>";
            a[++i] = " <td colspan='2' align='center'> ";
            a[++i] = " <form name='fred' action=''>";
            a[++i] = " <input type='text' name='aTxt' width='50'>";
            a[++i] = " <input type='button' value='click me'"
            + "
            onclick=\"alert (this.form.aTxt .value);\"><br> ";
            a[++i] = " <input type='reset' value='Reset'>" ;
            a[++i] = " </form>";
            a[++i] = " </td>";
            a[++i] = " </tr>";
            a[++i] = "</table>";

            var s = a.join("");

            document.write( s);

            </script>
            </body>
            </html>

            Comment

            • datactrl

              #7
              Re: create a web page with javascript on fly

              Thank's Rob. My question is that I would like to popup a web page from an
              already displayed page. And the popup page would like to be created from the
              already displayed page with javascript. I think the way using
              document.write( ) will change the content of already displayed page. That is
              not the question.

              I've tried 'new ActiveXObject(" InternetExplore r.Application") ;' sample
              provided by McKirahan. I think that would let me open an another window for
              the created page. But I got an "automation error" when I run this sample.
              Don't know what it is.

              Jack


              "RobG" <rgqld@iinet.ne t.auau> wrote in message
              news:Uyp9d.1962 $aA.60695@news. optus.net.au...[color=blue]
              > Have a search for a message subject "writing slabs of htm".
              > It was conclusively shown by Lasse that it is much quicker
              > to write HTML as elements in array, then use join("") to
              > concatenate them before writing to the page. The length of
              > time taken using += verus join() is exponentially longer,
              > getting seriously long for big pages.
              >
              > There is an example of how to use it below.
              >
              > Of course, if your content is only small...
              >
              > Cheers, Rob.
              >
              > <html>
              > <head>
              > <title>Slabs of HTML</title>
              > </head>
              > <body>
              > <script type="text/javascript">
              > var a = [];
              > var i = 0;
              > a[0] = " <p>here is paragraph " + i + "</p>";
              > a[++i] = " <p>here is paragraph " + i + "</p>";
              > a[++i] = " <p>here is paragraph " + i + "</p>";
              > a[++i] = " <table border='1' cellpadding='5' cellspacing='10 '>";
              > a[++i] = " <tr>";
              > a[++i] = " <td>This is a cell</td>";
              > a[++i] = " <td>another cell</td>";
              > a[++i] = " </tr>";
              > a[++i] = " <tr>";
              > a[++i] = " <td>This is a cell</td>";
              > a[++i] = " <td>another cell</td>";
              > a[++i] = " </tr>";
              > a[++i] = " <tr>";
              > a[++i] = " <td colspan='2' align='center'> ";
              > a[++i] = " <form name='fred' action=''>";
              > a[++i] = " <input type='text' name='aTxt' width='50'>";
              > a[++i] = " <input type='button' value='click me'"
              > + "
              > onclick=\"alert (this.form.aTxt .value);\"><br> ";
              > a[++i] = " <input type='reset' value='Reset'>" ;
              > a[++i] = " </form>";
              > a[++i] = " </td>";
              > a[++i] = " </tr>";
              > a[++i] = "</table>";
              >
              > var s = a.join("");
              >
              > document.write( s);
              >
              > </script>
              > </body>
              > </html>[/color]


              Comment

              • Andrey

                #8
                Re: create a web page with javascript on fly

                datactrl wrote:
                [color=blue]
                > Thank's Rob. My question is that I would like to popup a web page from an
                > already displayed page. And the popup page would like to be created from the
                > already displayed page with javascript. I think the way using
                > document.write( ) will change the content of already displayed page. That is
                > not the question.[/color]

                If you want to open a modal dialog from the newly generated page, just include an onLoad evend in
                <body> tag:

                ....
                a[0] = "<body onLoad='javascr ipt:showModalDi alog(<your parameters>)'>"
                ....
                a[<last>] = "</body>"

                [color=blue]
                > I've tried 'new ActiveXObject(" InternetExplore r.Application") ;' sample
                > provided by McKirahan. I think that would let me open an another window for
                > the created page. But I got an "automation error" when I run this sample.
                > Don't know what it is.
                >[/color]

                It should be a security issue, if you run this from a web server, which is not on your local
                machine. So if you want to have a web server where clients access remotely, you shouldn't use
                ActiveXObject - just use document.write.

                [color=blue]
                > Jack
                >
                >
                > "RobG" <rgqld@iinet.ne t.auau> wrote in message
                > news:Uyp9d.1962 $aA.60695@news. optus.net.au...
                >[color=green]
                >> Have a search for a message subject "writing slabs of htm".
                >> It was conclusively shown by Lasse that it is much quicker
                >> to write HTML as elements in array, then use join("") to
                >> concatenate them before writing to the page. The length of
                >> time taken using += verus join() is exponentially longer,
                >> getting seriously long for big pages.
                >>
                >> There is an example of how to use it below.
                >>
                >> Of course, if your content is only small...
                >>
                >> Cheers, Rob.
                >>
                >><html>
                >><head>
                >><title>Slab s of HTML</title>
                >></head>
                >><body>
                >><script type="text/javascript">
                >>var a = [];
                >>var i = 0;
                >>a[0] = " <p>here is paragraph " + i + "</p>";
                >>a[++i] = " <p>here is paragraph " + i + "</p>";
                >>a[++i] = " <p>here is paragraph " + i + "</p>";
                >>a[++i] = " <table border='1' cellpadding='5' cellspacing='10 '>";
                >>a[++i] = " <tr>";
                >>a[++i] = " <td>This is a cell</td>";
                >>a[++i] = " <td>another cell</td>";
                >>a[++i] = " </tr>";
                >>a[++i] = " <tr>";
                >>a[++i] = " <td>This is a cell</td>";
                >>a[++i] = " <td>another cell</td>";
                >>a[++i] = " </tr>";
                >>a[++i] = " <tr>";
                >>a[++i] = " <td colspan='2' align='center'> ";
                >>a[++i] = " <form name='fred' action=''>";
                >>a[++i] = " <input type='text' name='aTxt' width='50'>";
                >>a[++i] = " <input type='button' value='click me'"
                >> + "
                >>onclick=\"ale rt(this.form.aT xt.value);\"><b r>";
                >>a[++i] = " <input type='reset' value='Reset'>" ;
                >>a[++i] = " </form>";
                >>a[++i] = " </td>";
                >>a[++i] = " </tr>";
                >>a[++i] = "</table>";
                >>
                >>var s = a.join("");
                >>
                >>document.writ e(s);
                >>
                >></script>
                >></body>
                >></html>[/color]
                >
                >
                >[/color]

                Comment

                • RobG

                  #9
                  Re: create a web page with javascript on fly

                  datactrl wrote:[color=blue]
                  > Thank's Rob. My question is that I would like to popup a web page from an
                  > already displayed page. And the popup page would like to be created from the
                  > already displayed page with javascript. I think the way using
                  > document.write( ) will change the content of already displayed page. That is
                  > not the question.[/color]
                  [snip]

                  If you want the content in a new window, then create a new window called
                  (say) newWindow and write to it:

                  newWindow = window.open('', 'Window Title','paramet ers....');

                  ...

                  slabs of HTML

                  ...

                  newWindow.docum ent.write(s);



                  Then the HTML appears in newWindow, not the current one.


                  Cheers, Rob.

                  Comment

                  • Lee

                    #10
                    Re: create a web page with javascript on fly

                    RobG said:[color=blue]
                    >
                    >datactrl wrote:[color=green]
                    >> Thank's Rob. My question is that I would like to popup a web page from an
                    >> already displayed page. And the popup page would like to be created from the
                    >> already displayed page with javascript. I think the way using
                    >> document.write( ) will change the content of already displayed page. That is
                    >> not the question.[/color]
                    >[snip]
                    >
                    >If you want the content in a new window, then create a new window called
                    >(say) newWindow and write to it:
                    >
                    > newWindow = window.open('', 'Window Title','paramet ers....');
                    >
                    > ...
                    >
                    > slabs of HTML
                    >
                    > ...
                    >
                    > newWindow.docum ent.write(s);
                    >
                    >
                    >
                    >Then the HTML appears in newWindow, not the current one.[/color]

                    Sometimes. But sometimes you get an error because newWindow.docum ent
                    has no properties, because the O/S hasn't opened the new window fast
                    enough, so you're trying to write into a window that isn't ready.

                    globalHTML=s;
                    window.open("ja vascript:opener .globalHTML");

                    is more reliable.

                    Comment

                    • Dr John Stockton

                      #11
                      Re: create a web page with javascript on fly

                      JRS: In article <Uyp9d.1962$aA. 60695@news.optu s.net.au>, dated Fri, 8
                      Oct 2004 05:27:16, seen in news:comp.lang. javascript, RobG
                      <rgqld@iinet.ne t.auau> posted :
                      [color=blue]
                      >var a = [];
                      >var i = 0;
                      >a[0] = " <p>here is paragraph " + i + "</p>";
                      >a[++i] = " <p>here is paragraph " + i + "</p>";
                      >a[++i] = " <p>here is paragraph " + i + "</p>";[/color]

                      If you use i++ rather than ++i, then the first line can have the same
                      LHS as the rest, and the first para will be paragraph 1 - ISTM.

                      How about
                      var s = [
                      " <p>here is paragraph " + 1 + "</p>",
                      " <p>here is paragraph 2 </p>",
                      " <p>here is paragraph 3 </p>",
                      ...
                      "" ].join("") ;

                      taking the view that self-numbering paragraphs are probably not needed?

                      The empty string is to allow the previous line to end, like the rest,
                      with a comma, which seems nicer.

                      I've not tested for speed.

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                      Comment

                      • datactrl

                        #12
                        Re: create a web page with javascript on fly

                        Now I do as following:
                        <html>
                        <head>
                        <title>pop up a window on fly</title>
                        <script type="text/javascript">
                        //function PopUpWindow(){
                        var sHtm = ["<html>",
                        "<head>",
                        "<title>pop up htm</title>",
                        "</head>",
                        "<body>",
                        "<h1>Hello World</h1>",
                        "</body>",
                        "</html>"].join("");
                        // var s1 =
                        "dialogHeight=1 60px;dialogWidt h=300px;help=no ;status=no;resi zeable=no";
                        // window.showMode lessDialog("jav ascript:opener. sHtm","",s1);
                        window.open("ja vascript:opener .sHtm");
                        //}
                        </script>
                        </head>
                        <body>
                        <p>This is the calling window</P>
                        <button onClick="PopUpW indow()">popup</button>
                        </body>
                        </html>

                        This one works. But if I use button click to run PopUupWindow() or use
                        showModelessDia log() it won't work at all.

                        Jack


                        Comment

                        • Lee

                          #13
                          Re: create a web page with javascript on fly

                          datactrl said:[color=blue]
                          >
                          >Now I do as following:
                          ><html>
                          ><head>
                          ><title>pop up a window on fly</title>
                          ><script type="text/javascript">
                          >//function PopUpWindow(){
                          > var sHtm = ["<html>",
                          > "<head>",
                          > "<title>pop up htm</title>",
                          > "</head>",
                          > "<body>",
                          > "<h1>Hello World</h1>",
                          > "</body>",
                          > "</html>"].join("");
                          >// var s1 =
                          >"dialogHeight= 160px;dialogWid th=300px;help=n o;status=no;res izeable=no";
                          >// window.showMode lessDialog("jav ascript:opener. sHtm","",s1);
                          > window.open("ja vascript:opener .sHtm");
                          >//}
                          ></script>
                          ></head>
                          ><body>
                          ><p>This is the calling window</P>
                          ><button onClick="PopUpW indow()">popup</button>
                          ></body>
                          ></html>
                          >
                          >This one works. But if I use button click to run PopUupWindow() or use
                          >showModelessDi alog() it won't work at all.[/color]

                          Because you've made sHtm *local* to the function PopUpWindow().
                          In order to work, it must be a *global* variable.
                          Get rid of the "var" keyword.

                          Comment

                          • datactrl

                            #14
                            Re: create a web page with javascript on fly

                            > Because you've made sHtm *local* to the function PopUpWindow().[color=blue]
                            > In order to work, it must be a *global* variable.
                            > Get rid of the "var" keyword.[/color]


                            Thanks a lot Lee. Once sHtm is made as global it works. By the way, how
                            about using ShowModalDialog () or showModelessDia log(). It always showed
                            "error: opener.sHtm is null or not an object".

                            Jack


                            Comment

                            • RobG

                              #15
                              Re: create a web page with javascript on fly

                              datactrl wrote:
                              [snip][color=blue]
                              > Thanks a lot Lee. Once sHtm is made as global it works. By the way, how
                              > about using ShowModalDialog () or showModelessDia log(). It always showed
                              > "error: opener.sHtm is null or not an object".[/color]
                              [snip]

                              Those are IE only functions and are therefore shunned by
                              anyone wishing to create platform independent code - which,
                              after all, was the point of the internet and WWW in the
                              first place.

                              Cheers, Rob.

                              Comment

                              Working...