homepage Welcome to WebmasterWorld Guest from 54.205.207.53
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
using document.write to make code smaller
piggybank1974

10+ Year Member



 
Msg#: 769 posted 7:47 pm on Apr 10, 2003 (gmt 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..

 

DrDoc

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



 
Msg#: 769 posted 9:10 pm on Apr 10, 2003 (gmt 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\";" )
}

piggybank1974

10+ Year Member



 
Msg#: 769 posted 9:33 pm on Apr 10, 2003 (gmt 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.

DrDoc

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



 
Msg#: 769 posted 9:42 pm on Apr 10, 2003 (gmt 0)

Oh, well... do this then:

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

piggybank1974

10+ Year Member



 
Msg#: 769 posted 9:55 pm on Apr 10, 2003 (gmt 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..

DrDoc

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



 
Msg#: 769 posted 9:59 pm on Apr 10, 2003 (gmt 0)

if(document.getElementById) {
document.getElementById(String.fromCharCode(i)).style.visibility="hidden";
}
else if(document.all) {
document.all.String.fromCharCode(i).style.visibility="hidden";
}

piggybank1974

10+ Year Member



 
Msg#: 769 posted 10:29 pm on Apr 10, 2003 (gmt 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 ..

DrDoc

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



 
Msg#: 769 posted 10:50 pm on Apr 10, 2003 (gmt 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...

piggybank1974

10+ Year Member



 
Msg#: 769 posted 11:04 pm on Apr 10, 2003 (gmt 0)

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

thanks

the pig..

Xuefer

10+ Year Member



 
Msg#: 769 posted 2:02 am on Apr 11, 2003 (gmt 0)

if u insist, use eval() instead

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved