|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi guys....im new with html and javascript...i have little idea how to do this:
I have a table coded in html but I only want it to be put on the document on a if condition....but I dont know how to add chunk of html code in javascipt...its a big block of html...thanks in advance |
|
#2
|
||||
|
||||
|
Check into the document.write() function. I recommend devguru.com as a reference.
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#3
|
||||
|
||||
|
Hi ocean - welcome.
Inserting HTML into an HTML document via Javascript isn't really a recommended method. For example - you user may have Javascript turned off. To pragmatically control data in your page, you are much better off using a server-side language such as ASP, PHP, ASP.NET.... there's a few of 'em round these days. Failing that, if you REALLY want to mess with your HTML doc via JS, check out the "innerhtml" method, along with document.write. There is some great JS documentation here - http://devedge.netscape.com/central/javascript/ |
|
#4
|
|||
|
|||
|
Quote:
I tried document.write() like this : Code:
<script>
document.write("<table width = 400 align ="center"><tr><td>hello</td></tr></table>")
</script>
what am i doing wrong there? plz help |
|
#5
|
||||
|
||||
|
Since you need to begin and end the string parameter in the document.write method, you can't use it within the string, like you have with your align="center".. instead, use single quote (').
You should probably read a few tutorials/documents on Javascript & HTML just to get some knowledge of how functions, etc work. |
|
#6
|
||||
|
||||
|
Or use single quotes as your outside quotes and standard doubles inside. Or escape nested quotes by using the backslash character. Stumpy's right, of course, that you should strive not to have your content display dependent upon js. At the very least, you might look into using server side includes, if you don't have access to bona fide programming functionality. Most hosts nowadays offer PHP, though, so I'd be surprised if you couldn't do a quick PHP if statement with different includes.
|
|
#7
|
|||
|
|||
|
I really think that I need to learn php too....can you tell me what I need to get started...do I need any sort of compilers...and plz tell me what free webhosts allow php..and what is a php parser...
![]() |
|
#8
|
||||
|
||||
|
Oh, I don't know about free Web hosts allowing PHP. That's not necessarily the case. There are a million PHP tutorials and books out there. Take your pick. No compilers are necessary; a host just has to allow PHP pages and you're good to go.
|
|
#9
|
||||
|
||||
|
If the whole document.write() thing seems clumsy to you (and no, I doubt you'll find PHP on a free-hosting site) then there's another way of doing all this.
Either with CSS or inline tags (I'll explain both as you're new to HTML/JS) you can tell the table not to be displayed by default. Then you let your JavaScript display it on the IF condition (or if you're worried about non-JS browsers, have it shown and hide it with the JS if script condition not met, which is a better compromise but uglier) how you do this is: CSS: in the <head></head> of your document, put the following: <style> .myTable { display: none; } </style> then you write your table code as follows: <table id='conditionaltable' class="myTable" ... > <tr><td> whatever </td><td> whatever </td></tr> </table> If you don't like taking the <style> tags by the horns as yet, you can also use: <table id='conditionaltable' style="display: none;" .... > etc. </table> Then in your JavaScript (making sure that the table has been drawn by the time the script is called - ie in <body onload="">) you put a function that locates the table and sets that "display:none" attribute to "display:block". This can be done two ways - one is more reliable but requires you to have script for telling which browser the end-user has (there's plenty available - search google for "javascript browser sniffer") - that solution is: if Internet Explorer: document.all['conditionaltable'].style.display = "block"; (if that fails, try document.all.conditionaltable.style = "block"; - I could be wrong) if Netscape / similar: var myObj = document.getElementByID("conditionaltable"); myObj.style.display = "block"; the other solution depends on you knowing how many tables are in your document. If it's the only one, then all well and good. In any case, assuming it's the only (or else the first to be rendered), use: document.tables[0].style.display = "block"; Lastly, if you want to do the thing I suggested earlier of having it displayed unless not needed, just swap all references to "none" with the references to "block" Think that's it - any problems do PM or email me Al Last edited by almcnicoll : February 6th, 2004 at 06:39 PM. Reason: inaccuracies and spelling! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > HI plz help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|