7 Replies - 387 Views - Last Post: 18 July 2012 - 10:59 PM

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Where does it go?

Posted 18 July 2012 - 11:33 AM

I have yet another stupid question.. where does the JS go in a simple html page?

I'm taking a beginner class. Sometimes the instructor puts inside the the head tags, sometimes he puts it inside the body tags. As an example, I am working on an assignment where you use the navigator object to display some things about the browser. If that's all you were doing, where would you put the script code? I'm just curious about best programming practice, since he told me it didn't matter.

<html>
<head>
<title>Chapter 8 Assignment 1 </title>
<h1>Browser Settings:</h1>
<script type = "text/javascript">

if (navigator.javaEnabled() == true){
document.write("<h2>Java is enabled! </h2>");}
else{
document.write("<h2>Java is disabled! </h2>");}


document.write("<h2>Browser Platform: ", navigator.platform,"</h2>");
document.write("<h2>Browser Code Name: ", navigator.appCodeName,"</h2>");
document.write("<h2>Browser Name: ", navigator.appName,"</h2>");+
document.write("<h2>Browser Version: ", navigator.appVersion,"</h2>");

var name = prompt("Please enter your full name:","");
var age = prompt("Please enter your age:","");
var name = prompt("Please enter your gender as a single character \n(ex: M or m, F or f):","");


</script> 
</head>

<body bgcolor = "#4682B4">

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

</script>
</body>
</html>

This post has been edited by synlight: 18 July 2012 - 11:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Where does it go?

#2 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 448
  • View blog
  • Posts: 3,109
  • Joined: 12-January 10

Re: Where does it go?

Posted 18 July 2012 - 11:42 AM

I usually put mine at the end of all my pages that way I know exactly where it is at all times.
Was This Post Helpful? 1
  • +
  • -

#3 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Where does it go?

Posted 18 July 2012 - 11:53 AM

View PostDarenR, on 18 July 2012 - 01:42 PM, said:

I usually put mine at the end of all my pages that way I know exactly where it is at all times.


Huh. That makes sense.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: Where does it go?

Posted 18 July 2012 - 11:54 AM

There are a few different ways to think about it.

If your code is just willy-nilly writing out text/ HTML using document.write you most certainly want to place the code where the HTML should wind up on your page. For instance, your code I would place somewhere in the body since it is using document.write.

Now, I also tend to frown upon using document.write because it does modify the page in a potentially bad way and needs to be run inline (as the page loads) to not potentially have unexpected results (like overwriting the entire page when outputting the additional content).

I have the tendency to place all javascript in the head tag as it is always in the same place and I can easily look for it. But this also requires that you take a few more things into account when writing the script. You won't necessarily have the page loaded when it is first parsed and run, this means that you shouldn't use document.get functions, or document.body since they will be undefined or return a null result. So you will need to use window.onload functions or event listeners for the load event. Placing your code at the very bottom will remove much of the problem here, but doesn't allow for pre-processing to occur as the page loads.
Was This Post Helpful? 1
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,174
  • Joined: 12-June 08

Re: Where does it go?

Posted 18 July 2012 - 12:05 PM

Shockingly he's right.. it really doesn't matter except that you be consistent. I personally dump my javascript into a separate file and just include that. My damn html headers are stupid busy as it is - why throw more in there?
Was This Post Helpful? 1
  • +
  • -

#6 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Where does it go?

Posted 18 July 2012 - 12:13 PM

View PostBetaWar, on 18 July 2012 - 01:54 PM, said:

Now, I also tend to frown upon using document.write because it does modify the page in a potentially bad way and needs to be run inline (as the page loads) to not potentially have unexpected results (like overwriting the entire page when outputting the additional content).



Yeah, it seems awfully clunky. I'll be glad to move on from it after this class. I have no idea what else to use, document.write is all that's in my book. I searched for any echo commands, but it seems like they don't exist in JS.


View Postmodi123_1, on 18 July 2012 - 02:05 PM, said:

Shockingly he's right.. it really doesn't matter except that you be consistent.


That is indeed shocking LOL.
Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: Where does it go?

Posted 18 July 2012 - 05:25 PM

Normally I get an instance of a DOM object (using whatever method you like, but I also normally use document.getElementById and then I make a lot of use of the innerHTML attribute that DOM objects have to change what they display.
Was This Post Helpful? 0
  • +
  • -

#8 Vodkacannon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 07-November 11

Re: Where does it go?

Posted 18 July 2012 - 10:59 PM

So you could do this:

//the script
document.getElementById('foo').innerHTML=""; //alternatively replace the quotes with any variable
//the html
<p id="foo"></p>


This wont mess the page up like document.write()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1