Simple Javascript Question

liunx

Guest
Hi,<br /><br /><br />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 <a href="http://www.kaulaiscool.com/random/js_problem.htm" target="_blank">http://www.kaulaiscool.com/random/js_problem.htm</a><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><html><br /><head><br />    <title></title><br />        <script type="text/javascript"><br />            <!--<br /><br />                function why()<br />                    {<br />                        document.writeln("sample text");<br />                    }<br /><br />            // --><br />        </script><br /></head><br /><body><br />    <script type="text/javascript"><br />        <!--<br />            document.write("Here is some" + why() );<br />        // --><br />    </script><br />    <noscript><br />        Your Browser does not support JavaScript.<br />    </noscript<br /></body><br /></html><!--c2--></div><!--ec2--><br /><br />Thanks!<!--content-->
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?<br /><br /><br />Thanks<!--content-->
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.<!--content-->
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():<br /><br />function why()<br />{<br /> return "sample text";<br />}<br /><br /><br />That should do the trick.<!--content-->
As a side note, I believe it's more correct to say:<br /><br /><script language="javascript"><br /><br />than it is to say:<br /><br /><script type="text/javascript"><!--content-->
No for XHTML it is type="text/javascript". language= is only for html 4.01<!--content-->
Thanks carbonize <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><br /><br />I had just looked that up and came here to apologize. You beat me to the correction.<!--content-->
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.<!--content-->
 
Back
Top