Welcome to WebmasterWorld Guest from 54.144.80.75

Forum Moderators: open

Message Too Old, No Replies

using document.write to make code smaller

     
7:47 pm on Apr 10, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 26, 2003
posts:19
votes: 0


hi there

I've been writing my website and it work very well but I want the code as small as possible.

So i wanted to convert some of it to DHTML

here is the code:

document.getElementById('A').style.visibility = "hidden";
document.getElementById('B').style.visibility = "hidden";
document.getElementById('C').style.visibility = "hidden";
document.getElementById('D').style.visibility = "hidden";
document.getElementById('E').style.visibility = "hidden";
document.getElementById('F').style.visibility = "hidden";
document.getElementById('G').style.visibility = "hidden";
document.getElementById('H').style.visibility = "hidden";
document.getElementById('I').style.visibility = "hidden";
document.getElementById('J').style.visibility = "hidden";
document.getElementById('K').style.visibility = "hidden";
document.getElementById('L').style.visibility = "hidden";
document.getElementById('M').style.visibility = "hidden";
document.getElementById('N').style.visibility = "hidden";
document.getElementById('O').style.visibility = "hidden";
document.getElementById('P').style.visibility = "hidden";
document.getElementById('Q').style.visibility = "hidden";
document.getElementById('R').style.visibility = "hidden";
document.getElementById('S').style.visibility = "hidden";
document.getElementById('T').style.visibility = "hidden";
document.getElementById('U').style.visibility = "hidden";
document.getElementById('V').style.visibility = "hidden";
document.getElementById('W').style.visibility = "hidden";
document.getElementById('X').style.visibility = "hidden";
document.getElementById('Y').style.visibility = "hidden";
document.getElementById('Z').style.visibility = "hidden";

I could use this also:

this.document.all.A.style.visibility = "hidden";
more code.....

The only difference is the control names A to Z but I just get error when i'm try to convert.

Here's what i'm using.

for (i=65; i<=90; i=i+1) <!-- A to Z //-->
{
document.write("document.getElementById(" + String.fromCharCode(i) + " ).style.visibility="hidden";" )
}

got any ideas?

thanks the pig..

9:10 pm on Apr 10, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


You forgot the single quotes, and to escape the double quotes:

for (i=65; i<=90; i=i+1) <!-- A to Z //-->
{
document.write("document.getElementById('" + String.fromCharCode(i) + "').style.visibility=\"hidden\";" )
}

9:33 pm on Apr 10, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 26, 2003
posts:19
votes: 0


the edited code that you gave me prints it in the frame

like this:

document.getElementById('A').style.visibility="hidden";document.getElementById('B').style.visibility="hidden";

I dont want this printed in the frame, I want it to make the control visible or hidden

the pig.

9:42 pm on Apr 10, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


Oh, well... do this then:

document.getElementById(String.fromCharCode(i)).style.visibility="hidden";

9:55 pm on Apr 10, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 26, 2003
posts:19
votes: 0


thanks that has worked, it seemed to be the ; symbol on the end

what about converting with then?

this.document.all.A.style.visibility = "hidden";

the pig..

9:59 pm on Apr 10, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


if(document.getElementById) {
document.getElementById(String.fromCharCode(i)).style.visibility="hidden";
}
else if(document.all) {
document.all.String.fromCharCode(i).style.visibility="hidden";
}
10:29 pm on Apr 10, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 26, 2003
posts:19
votes: 0


hi DrDoc

Thanks for all the help so can you use the parent object etc on the fly in DHTML then?

as this again just prints it to the frame:

This is the original code:
parent.mainframe.document.location="menuframe/maths/b/b.html"

but i want to change it to like this:

press is a directory equal to 4 numeric
Letter is b

document.write('parent.mainframe.document.location=\"menuframe/+press+/+Letter+/+Letter+.html\"');

thanks again

the pig ..

10:50 pm on Apr 10, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


document.write always writes something to the browser. So, if you want the command to be executed you have to remove document.write.

Change
document.write('parent.mainframe.document.location=\"menuframe/+press+/+Letter+/+Letter+.html\"');

to
parent.mainframe.document.location="menuframe/"+press+"/"+Letter+"/"+Letter+".html";

Do you see and understand the difference?



document.write("alert()"); would write alert() to the window.

alert() will give you a JavaScript alert box...

11:04 pm on Apr 10, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 26, 2003
posts:19
votes: 0


are i thought you could use the document write command to manipulate an object also.

thanks

the pig..

2:02 am on Apr 11, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Nov 19, 2002
posts:372
votes: 0


if u insist, use eval() instead