Breaking up is hard to do.

Not really!

water1

JavaScript can be easily separated from  the HTML code.  To do this we just add a line of code  and your done.  Let look at an example in our example.  Here is the original code with some enhancement.

 <!DOCTYPE html>
<html lang=”en”>
<head>
<title>MyTest</title>
<meta charset=”utf-8″ />

function init()
{
document.getElementById(“panel”).innerHTML =”this is my test”
document.getElementById(“panel2″).innerHTML =”this is my test2”
}
</head>
<body onload = “init()”>
<p id = “panel2”></p>
</body>
</html>

If you copy this into note pad and save it as an html file, then you should get this when you open itmyTest

As you can see it just print two lines to the screen.  If we separate the scrip we will get the same results. Lets try it, note I have added a “bgcolor” to make it easier to read:

<!DOCTYPE html>
<html lang=”en”>
<head>
<title>MyTest</title>
<meta charset=”utf-8″ />

</head>
<p id = “panel2”></p>
</body>
</html>

And for the JavaScript:

function init()
{
document.getElementById(“panel”).innerHTML =”this is my test”
document.getElementById(“panel2″).innerHTML =”this is my test2”
}

This should be saved  as “myTest.js” in the same folder as the html file.  Here is the results:

myTest2

So there you go.  Next post we will get back to the data and maybe some styling.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s