473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can the code be simplified?

I have following page:

http://www.pinyinology.com/test/testGet.html

Part of the code is:

javascript:
document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";

html:

<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class='moon'in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.

Fulio Pen
Sep 19 '08 #1
13 1393
fulio pen wrote:
I have following page:

http://www.pinyinology.com/test/testGet.html

Part of the code is:

javascript:
document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";

html:

<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class='moon'in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.
I prefer not to use classes for arbitrary purposes. Rather, I create my own
attribute:

HTML:
<p moon></p>
<p moon></p>
....

javascript:
var moons = document.getElementsByAttribute("moon");
for(var m=0; m != moons.length; ++m)
moons[m].innerHTML = "moon";

Of course, document.getElementsByAttribute is not a standard function, but a
simple Google search will yield a number of implementations.

--
Josh
Sep 19 '08 #2
On Sep 18, 10:31*pm, Josh Sebastian <sebas...@gmail.comwrote:
fulio pen wrote:
I have following page:
http://www.pinyinology.com/test/testGet.html
Part of the code is:
javascript:
*document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";
html:
<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>
I wonder whether the code can be simplified, such as <class='moon'in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.

I prefer not to use classes for arbitrary purposes. Rather, I create my own
attribute:

HTML:
<p moon></p>
<p moon></p>
...

javascript:
var moons = document.getElementsByAttribute("moon");
for(var m=0; m != moons.length; ++m)
* moons[m].innerHTML = "moon";

Of course, document.getElementsByAttribute is not a standard function, but a
simple Google search will yield a number of implementations.

--
Josh
Josh:

Thanks a lot for your help.

Fulio Pen
Sep 19 '08 #3
Josh Sebastian wrote:
fulio pen wrote:
><p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class='moon'in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.
Search the archives for "getElementsByClass". You are welcome.

<http://jibbering.com/faq/>
I prefer not to use classes for arbitrary purposes. Rather, I create my own
attribute:

HTML:
So you prefer *invalid code* over defined microformats?
<p moon></p>
<p moon></p>
...
This is junk, not HTML.

<http://validator.w3.org/>
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Sep 19 '08 #4
SAM
fulio pen a écrit :
I have following page:

http://www.pinyinology.com/test/testGet.html

Part of the code is:

javascript:
document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";

html:

<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class='moon'in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.
All depends of what you want and how is your html code.

exo 1:
======
<html>
<script type="text/javascript">
window.onload = function() {
var p = document.getElementById("moon").getElementsByTagNa me('P');
for(var i=0, n=p.length; i<n; i++) p[i].innerHTML ="moon";
}
</script>
<body>
<p>exo 1</p>
<div id="moon">
<p></p>
<p></p>
<div>
<p>moom or not ?</p>
</div>
<p></p>
</div>
</body></html>
exo 2 :
=======
<html>
<script type="text/javascript">
window.onload = function() {
var p = document.body.getElementsByTagName('P');
for(var i=0, n=p.length; i<n; i++) p[i].innerHTML ="moon";
}
</script>
<body>
<p>exo 1</p>
<p></p>
<p></p>
<div>
<p>moom or not ?</p>
</div>
<p></p>
</body></html>

exo 3 :
=======
<html>
<script type="text/javascript">
window.onload = function() {
var obj = document.body.firstChild;
while (obj) {
if(obj.tagName && obj.tagName == 'P') obj.innerHTML = 'moon';
obj = obj.nextSibling;
}
}
</script>
<body>
<h1>test</h1>
<p></p>
<form>
<fieldset><legend>Fieldset</legend>
example: <input>
</fieldset>
<p><input type="submit"></p>
</form>
<p></p>
<div>
<p>not moon</p>
</div>
<p></p>
</body></html>
exo 4 :
=======
<html>
<script type="text/javascript">
window.onload = function() {
var obj = document.getElementsByTagName('DIV')[0].firstChild;
while (obj) {
if(obj.tagName && obj.tagName == 'P') obj.innerHTML = 'moon';
obj = obj.nextSibling;
}
}
</script>
<body>
<h1>test</h1>
<p>not moon</p>
<form>
<fieldset><legend>Fieldset</legend>
example: <input>
</fieldset>
<p><input type="submit"></p>
</form>
<p>not moon</p>
<div>
<p></p>
<p></p>
<p></p>
</div>
<p>not moon</p>
</body></html>

--
sm
Sep 19 '08 #5
On Sep 19, 1:44*am, fulio pen <fulio...@yahoo.comwrote:
document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";
(you could omit the first three of "moon"; )

function Wryt(ID, S) { document.getElementById(ID).innerHTML = S }
// generally useful; in an include file
//...
for (var J=1 ; J<=4 ; J++) Wryt("moon"+J, "moon")

If you have much like that, consider a variant of Wryt using a more
potent getElementBy... .

// Currently, my usual news service is write-only.

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ...
Sep 19 '08 #6
On Sep 19, 4:59*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Josh Sebastian wrote:

So you prefer *invalid code* over defined microformats?
I prefer clean solutions over hacks. Punning the class attribute is a
hack.
<p moon></p>
<p moon></p>
...

This is junk, not HTML.

<http://validator.w3.org/>
http://en.wikipedia.org/wiki/Document_Type_Definition
Sep 19 '08 #7
Josh Sebastian wrote:
Thomas 'PointedEars' Lahn wrote:
>Josh Sebastian wrote:

So you prefer *invalid code* over defined microformats?

I prefer clean solutions over hacks. Punning the class attribute is a
hack.
Even if so, using invalid markup is not a clean solution at all, especially
not in browser scripting.

<http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you>
>><p moon></p>
<p moon></p>
...
This is junk, not HTML.

<http://validator.w3.org/>

http://en.wikipedia.org/wiki/Document_Type_Definition
If only you had understood it.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 19 '08 #8
Josh Sebastian wrote:
On Sep 19, 4:59 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>Josh Sebastian wrote:

So you prefer *invalid code* over defined microformats?

I prefer clean solutions over hacks. Punning the class attribute is a
hack.
The class attribute widely supported. It is something that a developer
can be expected to understand.

However, this:-
>><p moon></p>
<p moon></p>
...
This is junk, not HTML.
It is not something developer (except the author) can be expected to
understand.

But to the OP's question at hand, I think innerHTML is the wrong choice
here. innerText/textContent is more efficient. The most efficient would
be to set the value of the text node, but this example does not have
text nodes. This can be solved by changing the markup a little.
<p class="moon"</p>

but for some browsers, the whitespace will be dropped, so:-

<p class="moon">&nbsp;</p>

would address the problem for those browsers. Once that's in place, we
can get an array of all elements with class "moon", loop through it, and
set the data for each <ptag's text node.

<script>
var moons = getElementsByClassName(document, "moon", "p");
for(var i = 0, len = moons.length; i < len; i++)
moons[i].firstChild.data = "moon" + i;
</script>

This would require a |getElementsByClassName| function.

It would be more efficient yet to get the moons like:-

<div id="moons">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>

<script type="text/javascript">
var moonsDiv = document.getElementById('moons'),
moons = moonsDiv.getElementsByTagName('p');
</script>

Which requires only a tiny bit of JavaScript and is valid markup.

Garrett
Sep 19 '08 #9
On Sep 19, 4:39*pm, dhtml <dhtmlkitc...@gmail.comwrote:
Josh Sebastian wrote:
On Sep 19, 4:59 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Josh Sebastian wrote:
So you prefer *invalid code* over defined microformats?
I prefer clean solutions over hacks. Punning the class attribute is a
hack.

The class attribute widely supported. It is something that a developer
can be expected to understand.

However, this:-
><p moon></p>
<p moon></p>
...
This is junk, not HTML.

It is not something developer (except the author) can be expected to
understand.

But to the OP's question at hand, I think innerHTML is the wrong choice
here. innerText/textContent is more efficient. The most efficient would
be to set the value of the text node, but this example does not have
text nodes. This can be solved by changing the markup a little.
<p class="moon"</p>

but for some browsers, the whitespace will be dropped, so:-

<p class="moon">&nbsp;</p>

would address the problem for those browsers. Once that's in place, we
can get an array of all elements with class "moon", loop through it, and
set the data for each <ptag's text node.

<script>
var moons = getElementsByClassName(document, "moon", "p");
for(var i = 0, len = moons.length; i < len; i++)
* *moons[i].firstChild.data = "moon" + i;
</script>

This would require a |getElementsByClassName| function.

It would be more efficient yet to get the moons like:-

<div id="moons">
* *<p>&nbsp;</p>
* *<p>&nbsp;</p>
* *<p>&nbsp;</p>
</div>

<script type="text/javascript">
var moonsDiv = document.getElementById('moons'),
* * *moons = moonsDiv.getElementsByTagName('p');
</script>

Which requires only a tiny bit of JavaScript and is valid markup.

Garrett
My sincere thanks to all for your your help. I will study the code
carefully.

fulio
Sep 19 '08 #10
On Sep 19, 10:44*am, fulio pen <fulio...@yahoo.comwrote:
I have following page:

http://www.pinyinology.com/test/testGet.html

Part of the code is:

javascript:
*document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";

html:

<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>

I wonder whether the code can be simplified, such as <class='moon'in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.
It depends on what you mean by simplified. The code as written is
pretty simple and would be understood by much anyone with an
understanding of HTML (javascript is not needed), I don't think it
could be any simpler.

If you mean can it be done with less code, that's possible if you
write supporting functions - but that means more code elsewhere.
There are a number of libraries that provide ways to create an array
of elements based on selectors, but you will then be adding a library
of perhaps 5,000 lines of code. And the resulting code would only be
understood by someone who knows that library - a basic understanding
of HTML or scripting would not be sufficient.
--
Rob
Sep 19 '08 #11
On Sep 19, 11:01*pm, Josh Sebastian <sebas...@gmail.comwrote:
On Sep 19, 4:59*am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Josh Sebastian wrote:
So you prefer *invalid code* over defined microformats?

I prefer clean solutions over hacks. Punning the class attribute is a
hack.
Thomas' suggestion is not a "hack", it is using the class attribute
for a legitimate purpose.
--
Rob
Sep 19 '08 #12
On Sep 19, 7:21*am, SAM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
fulio pen a écrit :
I have following page:
http://www.pinyinology.com/test/testGet.html
Part of the code is:
javascript:
*document.getElementById("moon1").innerHTML ="moon";
document.getElementById("moon2").innerHTML ="moon";
document.getElementById("moon3").innerHTML ="moon";
document.getElementById("moon4").innerHTML ="moon";
html:
<p id="moon1"></p>
<p id="moon2"></p>
<p id="moon3"></p>
<p id="moon4"></p>
I wonder whether the code can be simplified, such as <class='moon'in
the html section, and getElementsByClass in the javascript section.
Thanks for your expertise.

All depends of what you want and how is your html code.

exo 1:
======
<html>
<script type="text/javascript">
window.onload = function() {
var p = document.getElementById("moon").getElementsByTagNa me('P');
for(var i=0, n=p.length; i<n; i++) p[i].innerHTML ="moon";}

</script>
<body>
* *<p>exo 1</p>
* *<div id="moon">
* * *<p></p>
* * *<p></p>
* * *<div>
* * * *<p>moom or not ?</p>
* * *</div>
* * *<p></p>
* *</div>
</body></html>

exo 2 :
=======
<html>
<script type="text/javascript">
window.onload = function() {
var p = document.body.getElementsByTagName('P');
for(var i=0, n=p.length; i<n; i++) p[i].innerHTML ="moon";}

</script>
<body>
* *<p>exo 1</p>
* *<p></p>
* *<p></p>
* *<div>
* * *<p>moom or not ?</p>
* *</div>
* *<p></p>
</body></html>

exo 3 :
=======
<html>
<script type="text/javascript">
window.onload = function() {
var obj = document.body.firstChild;
while (obj) {
* *if(obj.tagName && obj.tagName == 'P') obj.innerHTML = 'moon';
* *obj = obj.nextSibling;
* *}}

</script>
<body>
* *<h1>test</h1>
* *<p></p>
* *<form>
* * *<fieldset><legend>Fieldset</legend>
* * * *example: <input>
* * *</fieldset>
* * *<p><input type="submit"></p>
* *</form>
* *<p></p>
* *<div>
* * *<p>not moon</p>
* *</div>
* *<p></p>
</body></html>

exo 4 :
=======
<html>
<script type="text/javascript">
window.onload = function() {
var obj = document.getElementsByTagName('DIV')[0].firstChild;
while (obj) {
* *if(obj.tagName && obj.tagName == 'P') obj.innerHTML = 'moon';
* *obj = obj.nextSibling;
* *}}

</script>
<body>
* *<h1>test</h1>
* *<p>not moon</p>
* *<form>
* * *<fieldset><legend>Fieldset</legend>
* * * *example: <input>
* * *</fieldset>
* * *<p><input type="submit"></p>
* *</form>
* *<p>not moon</p>
* *<div>
* * *<p></p>
* * *<p></p>
* * *<p></p>
* *</div>
* *<p>not moon</p>
</body></html>

--
sm
Hi, sm:

They all work well. I need to study the code carefully. Thanks.

fulio
Sep 19 '08 #13
On Sep 19, 4:39*pm, dhtml <dhtmlkitc...@gmail.comwrote:
Josh Sebastian wrote:
On Sep 19, 4:59 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Josh Sebastian wrote:
So you prefer *invalid code* over defined microformats?
I prefer clean solutions over hacks. Punning the class attribute is a
hack.

The class attribute widely supported. It is something that a developer
can be expected to understand.

However, this:-
><p moon></p>
<p moon></p>
...
This is junk, not HTML.

It is not something developer (except the author) can be expected to
understand.

But to the OP's question at hand, I think innerHTML is the wrong choice
here. innerText/textContent is more efficient. The most efficient would
be to set the value of the text node, but this example does not have
text nodes. This can be solved by changing the markup a little.
<p class="moon"</p>

but for some browsers, the whitespace will be dropped, so:-

<p class="moon">&nbsp;</p>

would address the problem for those browsers. Once that's in place, we
can get an array of all elements with class "moon", loop through it, and
set the data for each <ptag's text node.

<script>
var moons = getElementsByClassName(document, "moon", "p");
for(var i = 0, len = moons.length; i < len; i++)
* *moons[i].firstChild.data = "moon" + i;
</script>

This would require a |getElementsByClassName| function.

It would be more efficient yet to get the moons like:-

<div id="moons">
* *<p>&nbsp;</p>
* *<p>&nbsp;</p>
* *<p>&nbsp;</p>
</div>

<script type="text/javascript">
var moonsDiv = document.getElementById('moons'),
* * *moons = moonsDiv.getElementsByTagName('p');
</script>

Which requires only a tiny bit of JavaScript and is valid markup.

Garrett
Hi, Garrett:

Thanks for your help. I put your code in your message into two html
pages, as I wanted to understand them. But neither work. So I uploaded
them to my web site.

First one:
http://www.pinyinology.com/test/moonByG.html

code:

<script type="text/javascript">
var moonsDiv = document.getElementById('moons');
moons = moonsDiv.getElementsByTagName('p');
</script>

</head>
<body>
<div id="moons">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>

2nd one:
http://www.pinyinology.com/test/moonByG2.html

code:

script type="text/javascript">

var moons = getElementsByClassName(document, "moon", "p");
for(var i = 0, len = moons.length; i < len; i++)
moons[i].firstChild.data = "moon" + i;

</script>

</head>
<body>

<p class="moon"</p>
<p class="moon"</p>

<p class="moon">&nbsp;</p>
<p class="moon">&nbsp;</p>
</body>

Could you tell me how to modify the code? Thanks again.

Fulio
Sep 20 '08 #14

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

Similar topics

3
by: Stephan Kurpjuweit | last post by:
Hi! I am looking for a simplified Docbook XML schema. As far as I know, "Simplified Docbook" only exists as DTD, right? Could you please send me pointers or ideas that might be useful for me? ...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
25
by: Jon Slaughter | last post by:
I have some code that loads up some php/html files and does a few things to them and ultimately returns an html file with some php code in it. I then pass that file onto the user by using echo. Of...
1
by: Aprile | last post by:
Hello, everyone. I would like to ask something about searching Chinese simplified characters from the database. Every time I searched, it won't display data, but if I searched without a condition it...
0
by: scriptmann | last post by:
Hi, I'm trying to use os.listdir() to list directories with simplified chinese filenames. However, while I can see the filenames correctly in windows explorer, I am getting ? in the filename...
7
by: RichB | last post by:
I am just trying to get to grips with C# and OOP, and one of the benefits would seem to be code reuse. However I am not sure where to draw the line. I have the following section of code: if...
7
by: Zytan | last post by:
I have the simplified build ("show advanced build configurations" turned off), so that pressing F5 runs in DEBUG mode with the debugger. When an assertion fires, I find that I cannot 'watch' some...
3
by: jduhler | last post by:
Is there a way in SQL Server T-SQL to store commonly used SQL statements in a function, stored proc, or system variable? That way if that code ever changes I can change it in one place. If I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
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...

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.