Kaula Posted October 11, 2006 Posted October 11, 2006 Hi, Heres some basic code, what I'm trying to do is pull some text from a function in the head section but you can see it seems to be showing up before its called. Not sure If I have my terminology right. Am I doing something wrong? Heres the example of what it does in a browser http://www.kaulaiscool.com/random/js_problem.htm ><html> <head> <title></title> <script type="text/javascript"> <!-- function why() { document.writeln("sample text"); } // --> </script> </head> <body> <script type="text/javascript"> <!-- document.write("Here is some" + why() ); // --> </script> <noscript> Your Browser does not support JavaScript. </noscript </body> </html> Thanks! Quote
Kaula Posted October 11, 2006 Author Posted October 11, 2006 I just removed the why() and added it outside of the document.write in the body and it works fine, Is that the only way to do it? Thanks Quote
carbonize Posted October 11, 2006 Posted October 11, 2006 I'm not sure but I don't think you can call a function from in a string like that or possibly just not during a document.write. Quote
nortk Posted October 11, 2006 Posted October 11, 2006 Your treating why() as if it IS a string. But why() actually PRINTS a string. What you had essentially put a writeln inside a write. Modify your why() function to say this, instead of the writeln(): function why() { return "sample text"; } That should do the trick. Quote
nortk Posted October 11, 2006 Posted October 11, 2006 As a side note, I believe it's more correct to say: <script language="javascript"> than it is to say: <script type="text/javascript"> Quote
carbonize Posted October 11, 2006 Posted October 11, 2006 No for XHTML it is type="text/javascript". language= is only for html 4.01 Quote
nortk Posted October 11, 2006 Posted October 11, 2006 Thanks carbonize I had just looked that up and came here to apologize. You beat me to the correction. Quote
Kaula Posted October 12, 2006 Author Posted October 12, 2006 Thanks for the responses, I got it all working properly now. Normally I use xhtml, I just whipped that up real quick as an example, forgot that the two where different. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.