drmbdrummer
11-17-2003, 12:41 PM
i wrote my entire webpage using the document.write command, but now i need to create a functino inside the body tags, is this possible, and if so how on earth is it done, thank you
|
Click to See Complete Forum and Search --> : calling scripts inside body tags? drmbdrummer 11-17-2003, 12:41 PM i wrote my entire webpage using the document.write command, but now i need to create a functino inside the body tags, is this possible, and if so how on earth is it done, thank you David Harrison 11-17-2003, 03:02 PM May I suggest that you don't write your entire page in JavaScript, if you do that then people with JavaScript disabled or people using non-visual browsers cannot "view" your page. In any case, here's how it's done: document.write('<script type="text/javascript"><!--'); document.write('a_function();'); document.write('</sc'+'ript>'); fredmv 11-17-2003, 03:04 PM My preferred method (escaping closing <script> tag): document.write('<script type="text/javascript"><!--'); document.write('a_function();'); document.write('//--><\/script>'); David Harrison 11-17-2003, 03:07 PM Oops, forgot the //--> fredmv 11-17-2003, 03:08 PM Originally posted by lavalamp Oops, forgot the //--> Yeah, that's why I made it bold and blue. :D David Harrison 11-17-2003, 03:10 PM I noticed. Charles 11-17-2003, 03:11 PM What's up with the HTML comments? David Harrison 11-17-2003, 03:12 PM It's to hide the script for browsers with JavaScript disabled. I would have thought that you would have known that one Charles. fredmv 11-17-2003, 03:13 PM Originally posted by Charles What's up with the HTML comments? I believe it's used since, in browsers that don't support JavaScript, they actually print out all of the code in-between the <script> tags, by commenting them out prevents this. A lot of people also use this same technique for internal CSS. Charles 11-17-2003, 03:21 PM Then why are you using it inside of a "document.write()"? David Harrison 11-17-2003, 03:23 PM Good point, habit I suppose. It's good practice for when it is needed though. Charles 11-17-2003, 03:30 PM Originally posted by lavalamp It's good practice for when it is needed though. But dangerous in this case. Consider... <script type="text/javascript"> <!-- document.write('<script type="text/javascript"><!--'); ... document.write('// --></script>'); // --> </script> Now, SGML has a lot of rules the browsers ignore and one of them is the permissibility of nested comments. And the browsers that do not understand the SCRIPT element are the ones that you can expect to have trouble with nested comments. To such a browser the above looks like... <!-- document.write('<!--'); ... document.write('// -->'); // --> David Harrison 11-17-2003, 03:34 PM Best just to stick with this then: document.write('<script type="text/javascript">'); document.write('a_function();'); document.write('</sc'+'ript>'); or: document.write('<\/script>'); A good spot Charles. How are you not a moderator yet? fredmv 11-17-2003, 03:39 PM Originally posted by lavalamp A good spot Charles. How are you not a moderator yet? I've always thought he was... webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |