1

I'm trying to create this webpage for homework where I have to state which function I'm testing and give the result after. For example:

 "Testing function1" --> should be a heading (h1....h6)
 result 
 "Testing function2"
 result1
 ...

According to the homework rules, i have to put my javascript code in head and call the functions in body.

I tried thinking of a way where I don't need to re-type

   <script type="text/javascript">  </script> 

a few times. Any ideas?

user977151
  • 495
  • 5
  • 9
  • 18
  • Doesn't need it, he's calling the functions in the body and just defining them in the head – Joe Feb 24 '12 at 13:26

1 Answers1

1

There's nothing wrong with having <script.../script> a few times, if that's the order it appears in the page. You don't need to group all your scripts into one <script> tag for neatness or anything.

It's pretty common to see a block of HTML then a <script> after it which does something relating to that HTML (bad practice yes, but not something to worry about just yet when you're learning).

Unless there's something specifically saying you can't use more than one inline script, I'd just say make it easy for yourself.

You could use some document.writes if you really wanted to, or something along those lines (.innerHTML is another option) but that'll just generate messier code. Keep your HTML as HTML, and where possible, don't write Javascript that creates HTML.


All in one:

<script type="text/javascript">
  document.write('<h1>Testing f1()</h1><p>' + function1() + '</p><h1>Testing f2()</h1><p>' + function2() + '</p>');
</script>

Yes it's one line, but damn, how annoying would that be to debug if it was something a little more complex?


My recommendation:

<h1>Testing f1()</h1>
<p>
    <script type="text/javascript">
        function1();
    </script>
</p>

<h1>Testing f2()</h1>
<p>
    <script type="text/javascript">
        function2();
    </script>
</p>

Yes it's more lines, but it's much easier to see what it's doing at a glance - you don't need to actually read it, it's very very obvious.

Joe
  • 15,669
  • 4
  • 48
  • 83
  • So you won't recommend something like this: document.write("

    Testing function1()...

    " + function1() + "

    "); I tried it that way inside the script tags, but the h3 tags and p tags won't validate.
    – user977151 Feb 24 '12 at 14:01
  • No I wouldn't, for the simple reason that putting HTML in your JS when it doesn't need to be is silly :) What if you want an image tag? `document.write('');` - that's fine. Now how about CSS? `document.write('');` it gets messy quickly – Joe Feb 24 '12 at 14:43
  • Thank you:) Also I noticed that some examples require the code in script tags to be inside html comments. I don't know if this is consider cheating or just a simple way to avoid errors with validation. – user977151 Feb 24 '12 at 14:55
  • See http://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag for info on that issue - never use HTML comments (``), instead use CDATA sections (see link). They go in the same place, and kind of do the same thing :) Also see http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work :P – Joe Feb 24 '12 at 15:05
  • Hey, I hope you don't mind me asking something else. The question tells me not to use global variables. Im a bit confused on how to call the function in body section. Does the variables I create in body section count as global variable? If it does, then how do I call the functions that needs a parameter? – user977151 Feb 25 '12 at 21:31
  • As a general rule of thumb, any variable not defined inside a function is a global variable. There's a couple of exceptions, but don't worry about them. Do you need to pass anything from the body to the function? Don't forget that you can read the HTML elements in the `` from inside the function, and get attributes and values from them, if that's what you need (look into `document.getElementById()` ) – Joe Feb 25 '12 at 21:53
  • My functions require an array or a string as parameter. So I was making a new variable and putting them as parameter for each function. But I guess I have to skip making a new variable and just insert the array/string to function itself. – user977151 Feb 25 '12 at 22:15