473,766 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing slabs of html

hi ppl

i've made a javascript component in a html file, and i want to be able
to use this on other pages, so i'm going to put it into a js file.
thing is, the component in 90% html and 10% java. so how do i print out
a whole slab of html without having to do a hideous batch of
document.write? couldn't figure out how other components did it.
cheers
dave

Jul 23 '05 #1
12 1572
<ma**********@h otmail.com> wrote in message
news:10******** **************@ h37g2000oda.goo glegroups.com.. .
hi ppl

i've made a javascript component in a html file, and i want to be able
to use this on other pages, so i'm going to put it into a js file.
thing is, the component in 90% html and 10% java. so how do i print out
a whole slab of html without having to do a hideous batch of
document.write? couldn't figure out how other components did it.
cheers
dave

Put the Java in a separate .js file.

Also, to avoid numerous document.write statements,
use an array; for example, try something like:

var a = new Array();
a[i++] = "1";
a[i++] = "2";
a[i++] = "3";
for (i=0; i<a.length; i++) {
document.write a[i];
}

Jul 23 '05 #2
McKirahan wrote:
<ma**********@h otmail.com> wrote in message
news:10******** **************@ h37g2000oda.goo glegroups.com.. .
hi ppl

i've made a javascript component in a html file, and i want to be able
to use this on other pages, so i'm going to put it into a js file.
thing is, the component in 90% html and 10% java. so how do i print out
a whole slab of html without having to do a hideous batch of
document.writ e? couldn't figure out how other components did it.
cheers
dave
Put the Java in a separate .js file.


<pedant>
Java != Javascript
</pedant>

Also, to avoid numerous document.write statements,
use an array; for example, try something like:

var a = new Array();
a[i++] = "1";
a[i++] = "2";
a[i++] = "3";
for (i=0; i<a.length; i++) {
document.write a[i];
}


That doesn't avoid numerous document.write statements, it just avoids
the author having to type them. If the array has 10,000 items in it,
then the document.write is called 10,000 times. Its a lot more
efficient, and faster, to use concatenation and then one document.write:

var i=0;
var myVar = "";
var a = new Array();
a[i++] = "1";
a[i++] = "2";
a[i++] = "3";
for (i=0; i<a.length; i++) {
myVar += a[i];
}
document.write( myVar);

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #3
"Randy Webb" <Hi************ @aol.com> wrote in message
news:yY******** ************@co mcast.com...
McKirahan wrote:
<ma**********@h otmail.com> wrote in message
news:10******** **************@ h37g2000oda.goo glegroups.com.. .
hi ppl

i've made a javascript component in a html file, and i want to be able
to use this on other pages, so i'm going to put it into a js file.
thing is, the component in 90% html and 10% java. so how do i print out
a whole slab of html without having to do a hideous batch of
document.writ e? couldn't figure out how other components did it.
cheers
dave


Put the Java in a separate .js file.


<pedant>
Java != Javascript
</pedant>

Also, to avoid numerous document.write statements,
use an array; for example, try something like:

var a = new Array();
a[i++] = "1";
a[i++] = "2";
a[i++] = "3";
for (i=0; i<a.length; i++) {
document.write a[i];
}


That doesn't avoid numerous document.write statements, it just avoids
the author having to type them. If the array has 10,000 items in it,
then the document.write is called 10,000 times. Its a lot more
efficient, and faster, to use concatenation and then one document.write:

var i=0;
var myVar = "";
var a = new Array();
a[i++] = "1";
a[i++] = "2";
a[i++] = "3";
for (i=0; i<a.length; i++) {
myVar += a[i];
}
document.write( myVar);

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq


Well I did say "something like" :)

I actually meant to say what you did.
Jul 23 '05 #4
JRS: In article <yY************ ********@comcas t.com>, dated Sat, 2 Oct
2004 10:08:49, seen in news:comp.lang. javascript, Randy Webb
<Hi************ @aol.com> posted :

That doesn't avoid numerous document.write statements, it just avoids
the author having to type them. If the array has 10,000 items in it,
then the document.write is called 10,000 times. Its a lot more
efficient, and faster, to use concatenation and then one document.write:

var i=0;
var myVar = "";
var a = new Array();
a[i++] = "1";
a[i++] = "2";
a[i++] = "3";
for (i=0; i<a.length; i++) {
myVar += a[i];
}
document.write (myVar);


(1) document.write( a.join('')) // might be better

(2) Or
var myVar = "" +
"1" +
"2" +
"3" +
"" ;
document.write( myVar)

The average overhead of a few spaces, two quotes and a + per line of
HTML are not really important. Note that I've arranged matters so that
all 3 'HTML' lines are given identical treatment.

--
© 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.
Jul 23 '05 #5
Dr John Stockton <sp**@merlyn.de mon.co.uk> writes:
(1) document.write( a.join('')) // might be better


In the general case, it can be a *lot* better.
In the Javascript implementations I have checked, Array.prototype .join
works in time proportional to the size of the resulting string.
In comparison, doing manual concatenation in a loop can take time
proportional to the square of the size of the resulting string.

Example code:
---
var N = 10000;
var arr = [];
for (var i = 0; i < N; i++) {
arr[i] = "hello world!";
}
var d1 = new Date()
var r1 = "";
for (i = 0; i < N; i++) {
r1 += arr[i];
}
var d2 = new Date();
var r2 = arr.join("");
var d3 = new Date();
alert("Times: " + (d2-d1) + " vs. " + (d3-d2));
---

In IE 6, the result is 29072 ms vs. 20 ms, a factor of ... 1500.

Mozilla has 70 m vs 20 ms. Increasing N to 100000 gives ~6000 vs 5000,
so not nearly as big a fifference (hmm, the join might actually be
inefficiently created!).

In Opera 7, I had to increase N to a full million to get significant
numbers, 9133 ms vs 1433 ms., only a factor ~6.
In general: Creating a string by repeatedly appending to it is
inefficient. Don't do it. In Javascript, the "join" function is
an efficient way to join an array of strings. In, e.g., Java
you would use the StringBuffer class instead.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
but if i have 50-100 lines of code, concatenation or arrays is going to
get horrifically messy and painful to code. SURELY there must be a
better way of doing it. in PHP you can start HTML wherever and whenever
you like, you just close the php bit ?> and start it again when you
need <?php. Maybe I'm going about this the wrong way.

cheers
dave

Jul 23 '05 #7
Lasse Reichstein Nielsen wrote:
Dr John Stockton writes:
(1) document.write( a.join('')) // might be better


In the general case, it can be a *lot* better.
In the Javascript implementations I have checked, Array.prototype .join
works in time proportional to the size of the resulting string.
In comparison, doing manual concatenation in a loop can take time
proportional to the square of the size of the resulting string.

<snip>

Even if the internal operation of Array prototype join was an equivalent
concatenation loop it should be expected to be quicker than doing the
same in javascript as it will be 'native' code doing the work. But
obviously join has a sufficiently clear outcome to allow much internal
optimisation.

When I have proposed the use of join in this context in the past it has
been suggested that it is less clear in the source code, but I know that
repeated concatenation is so inefficient in comparison (probably due to
the creation (and subsequent destruction) of many intermediate strings)
that I use it extensively. Often implemented as an overloaded toString
property assigned to the array:-

function arrayToString() {

return this.join('');

}

.. . .

ar.toString = arrayToString;

- and then just reference the array in a context where it will be
type-converted to a sting (thus calling the replacement toStirng
method).

The function can be re-used, and arrays that employ it can be mixed in
other arrays with string literal elements to have the whole lot
concatenated and output in one go.

Richard.
Jul 23 '05 #8
maybe i could use the DOM, like ummm... telling it to document.write
the root node somehow.

just an idea.

cheers
dave

Jul 23 '05 #9
ma**********@ho tmail.com wrote:
maybe i could use the DOM, like ummm... telling it to document.write


I think you'll find DOM methods very lengthy, unless your using
cloneNode. But the fact is that at some time you have to write HTML to
the browser.

Using the suggested array/join method is not as bad as you think, e.g.:

<html>
<head>
<title>Fred</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>

Cheers, Fred.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2153
by: Christoph Bergmann | last post by:
Hi... We want to write an open source web based TEXT editor and would be happy about any help ;-) Please notice: We do NOT want to write a web based WEB editor, where you can edit a web page without knowing HTML - we want to write a real programmer's editor located on a web page in your browser with features like...
385
17283
by: Xah Lee | last post by:
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like extraneous jargons, such as in the Unix & Perl community. Unlike mathematicians, where in mathematics there are no fewer jargons but each and every one are
17
7685
by: Eric Lindsay | last post by:
Is learning to write CSS a better use of time than finding and using a package that produces complete web pages? I've moved to a new platform (Macintosh), taking with me about 400 personal web pages, some dating back so far I probably wrote them in vi. About 4 years ago (thanks in part to hints found in this group) I converted about 80 pages to CSS, and was fairly happy with the result, plain though they are. Since then I've forgotten...
3
2765
by: VMI | last post by:
I'm having trouble writing several records from an XML file into an html file. The problem is that it only outputs one record to the html file. I need to export all the records in the xml file to html. How can I do this? Is there a better (and easier) way to export a dataset to a 'friendly-printer' format? The resulting XML file is hard to understand if printed, and that's why I'm using XLST. This is what the xsl file looks like. Can I...
6
21440
by: guy | last post by:
Does .NET have a class or set of functions that facilitate the creating and writing of html files? I have been creating files in streams and constructing html strings and writing them out but it seems that a class could facilitate this better. Thanks
2
4496
by: Tony | last post by:
Yes, I need to specify a font type so that the characters will be evenly spaced when I write to a tab delimited text file. So how does one specify a font type to write/print and which font is best for evenness?
102
7121
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality of its documentation, it is a impeccability. it has or possesses properties of:
1
1805
by: spohle | last post by:
hi, i use a lot the enumerate in my scripts and got really interested in possibly writing my own enumerate as an extension, for which i would want to extend it to be able to pass a start and step attribute. can anyone point me on my way with good examples for that and how to write extensions ? thank you in advance
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.