469,312 Members | 2,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

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
Jul 23 '05 #1
16 22679
"datactrl" <qu***@tpg.com.au> wrote in message
news:41********@dnews.tpgi.com.au...
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


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.htm</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

You can create a page using javascript:

<html>
<head>
<title>test2.htm<title>
<script type="text/javascript">
var htm = "<html>";
htm += "<head>";
htm += "<title>test3.htm;
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.
Jul 23 '05 #2
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:Con9d.352861$Fg5.74302@attbi_s53...
"datactrl" <qu***@tpg.com.au> wrote in message
news:41********@dnews.tpgi.com.au...
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


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.htm</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

You can create a page using javascript:

<html>
<head>
<title>test2.htm<title>
<script type="text/javascript">
var htm = "<html>";
htm += "<head>";
htm += "<title>test3.htm;
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.


Of course
htm += "<title>test3.htm;
should have been
htm += "<title>test3.htm</title>";
Here's another possibility.

var htm = "<html>";
htm += "<head>";
htm += "<title>test4.htm</title>";
htm += "</head>";
htm += "<body>";
htm += "<h1>Hello World</h1>";
htm += "</body>";
htm += "</html>";
var oIE = new ActiveXObject("InternetExplorer.Application");
oIE.visible = true;
oIE.navigate("about:blank");
oIE.document.open;
oIE.document.write(htm);
oIE.document.close;
Jul 23 '05 #3
Lee
datactrl said:

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!


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?

Jul 23 '05 #4
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 showModelessDialog() to open it, is that
possible? Thanks again !

Jack

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:BAn9d.209130$D%.75621@attbi_s51...
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:Con9d.352861$Fg5.74302@attbi_s53...
"datactrl" <qu***@tpg.com.au> wrote in message
news:41********@dnews.tpgi.com.au...
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


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.htm</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

You can create a page using javascript:

<html>
<head>
<title>test2.htm<title>
<script type="text/javascript">
var htm = "<html>";
htm += "<head>";
htm += "<title>test3.htm;
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.


Of course
htm += "<title>test3.htm;
should have been
htm += "<title>test3.htm</title>";
Here's another possibility.

var htm = "<html>";
htm += "<head>";
htm += "<title>test4.htm</title>";
htm += "</head>";
htm += "<body>";
htm += "<h1>Hello World</h1>";
htm += "</body>";
htm += "</html>";
var oIE = new ActiveXObject("InternetExplorer.Application");
oIE.visible = true;
oIE.navigate("about:blank");
oIE.document.open;
oIE.document.write(htm);
oIE.document.close;

Jul 23 '05 #5
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>
Jul 23 '05 #6
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("InternetExplorer.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" <rg***@iinet.net.auau> wrote in message
news:Uy*****************@news.optus.net.au...
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>

Jul 23 '05 #7
datactrl wrote:
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.
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='javascript:showModalDialog(<your parameters>)'>"
....
a[<last>] = "</body>"

I've tried 'new ActiveXObject("InternetExplorer.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.

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.

Jack
"RobG" <rg***@iinet.net.auau> wrote in message
news:Uy*****************@news.optus.net.au...
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>


Jul 23 '05 #8
datactrl wrote:
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.

[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','parameters....');

...

slabs of HTML

...

newWindow.document.write(s);

Then the HTML appears in newWindow, not the current one.
Cheers, Rob.
Jul 23 '05 #9
Lee
RobG said:

datactrl wrote:
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.

[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','parameters....');

...

slabs of HTML

...

newWindow.document.write(s);

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


Sometimes. But sometimes you get an error because newWindow.document
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("javascript:opener.globalHTML");

is more reliable.

Jul 23 '05 #10
JRS: In article <Uy*****************@news.optus.net.au>, dated Fri, 8
Oct 2004 05:27:16, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :
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>";


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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11
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>popup htm</title>",
"</head>",
"<body>",
"<h1>Hello World</h1>",
"</body>",
"</html>"].join("");
// var s1 =
"dialogHeight=160px;dialogWidth=300px;help=no;stat us=no;resizeable=no";
// window.showModelessDialog("javascript:opener.sHtm" ,"",s1);
window.open("javascript:opener.sHtm");
//}
</script>
</head>
<body>
<p>This is the calling window</P>
<button onClick="PopUpWindow()">popup</button>
</body>
</html>

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

Jack
Jul 23 '05 #12
Lee
datactrl said:

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>popup htm</title>",
"</head>",
"<body>",
"<h1>Hello World</h1>",
"</body>",
"</html>"].join("");
// var s1 =
"dialogHeight=160px;dialogWidth=300px;help=no;sta tus=no;resizeable=no";
// window.showModelessDialog("javascript:opener.sHtm" ,"",s1);
window.open("javascript:opener.sHtm");
//}
</script>
</head>
<body>
<p>This is the calling window</P>
<button onClick="PopUpWindow()">popup</button>
</body>
</html>

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


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.

Jul 23 '05 #13
> 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.

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

Jack
Jul 23 '05 #14
datactrl wrote:
[snip]
Thanks a lot Lee. Once sHtm is made as global it works. By the way, how
about using ShowModalDialog() or showModelessDialog(). It always showed
"error: opener.sHtm is null or not an object".

[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.
Jul 23 '05 #15
Dr John Stockton wrote:

[snip]
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?


Looks great. I auto-numbered the paras just for the sake of
it, no real purpose.

Rob.
Jul 23 '05 #16
> Sometimes. But sometimes you get an error because newWindow.document
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("javascript:opener.globalHTML");

is more reliable.


This works fine. But sometimes it will open a blank window without any
content. Is there any way to work arround this problem? Thanks in advance!

Jack
Jul 23 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Axel Dahmen | last post: by
2 posts views Thread by theseer | last post: by
3 posts views Thread by GroupReader | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.