Move inline script into external js file

javascript.zip (3.3 KB)

Hi I was using inline JavaScript in my Html and internal css.
I made external files for the javascript and the css. My Css is loading beautifuly but my javascript is not. attached is my 3 files.

<script src="assignment-jasmine.js"></script> is the code I’m using to call my javascript file. Is this correct and where do I place it on my page?

This is my HTML

<!Doctype html>
<html>

<head>
 <link rel="stylesheet" href="assignment-Jasmine.css">

<title>JavaScript Assignments- Jasmine Lusty</title>

</head>

<body>

    <h2>JavaScript Assignment 5 - Jasmine Lusty</h2>
<!-- Grade Html -->
	<h4>Grades!</h4>
	
	<p>Select your age from the list.</p>

		<select id="mySelect" onchange="age()">
			<option value="Grade">Age</option>
			<option value="Grade 1">6</option>
			<option value="Grade 2">7</option>
			<option value="Grade 3">8</option>
			<option value="Grade 4">9</option>
			<option value="Grade 5">10</option>
			<option value="Grade 6">11</option>
		</select>


	<p id="selectedGrade"></p>
	<br>
<!-- Math Html -->
	<h4>Math!</h4>
	<p>Try to see if you can do the math</p>
		<table id="addition-table">
			<tbody>
				<tr class="Calculation">
					<td>5</td>
					<td>3</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>7</td>
					<td>6</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>5</td>
					<td>5</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>8</td>
					<td>3</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>4</td>
					<td>7</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>3</td>
					<td>9</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>8</td>
					<td>5</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>2</td>
					<td>6</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>5</td>
					<td>9</td>
					<td><input type="text"></td>
				</tr>
				<tr class="Calculation">
					<td>6</td>
					<td>6</td>
					<td><input type="text"></td>
				</tr>
			</tbody>
		</table>
	<br>
<!---- Email Html--->
	<h4>Email Validation!</h4>
	<p>Type in Your Email</p>
		<input type="email" id="email">
		<br>
		<br>
<script src="assignment-jasmine.js"></script>
</body>
</html>

This is my JavaScript

//Age/Grade Code
		 
		function age() {
  			var x = document.getElementById("mySelect").value;
					document.getElementById("selectedGrade").innerHTML = "You selected: " + x;
		}

//Math Code
		
		(function( math )  { 
				   'use strict';
				   var tr = math.querySelectorAll( 'tr' ), 
					   td = math.querySelectorAll( 'td' ), 
					  inp = math.querySelectorAll( 'input' ),
				   totals = [], c;
			   for ( c = 0; c < tr.length; c ++ ) {
					 totals.push( parseFloat( td[ c * 3 ].textContent ) + 
								  parseFloat( td[ c * 3 + 1 ].textContent ) );
					 inp[ c ].value = ' ';				   
					 inp[ c ].addEventListener( 'blur', check_answer( c ), false );

				}

		function check_answer( c ) {
			   inp[ c ].onblur = function() { 
				  if( inp[ c ].value == totals[ c ] ) {
					  tr[ c ].classList.add( 'right' );
					 }
						  else {
							 tr[ c ].classList.remove( 'right' );
							 tr[ c ].classList.add( 'wrong' );
							 }
						  }			   
					   }
		 }( document ) );  
//Email Code
		function validateEmail(email, emailPattern){

	    emailPattern = (emailPattern instanceof RegExp) ?  emailPattern : /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/;

		return matchResults;
}		

	
			var email = document.getElementById('email')

			email.addEventListener('blur', function (event) {
			  event.target.style.backgroundColor = event.target.validity.valid
				? 'transparent'
				: 'red'
			})
	
//remove code
	
	window.removeEventListener("beforeunload", "mySelect", false);
	window.removeEventListener("beforeunload", check_answer( c ), false );
	window.removeEventListener("beforeunload", function (event)

And this is my css code

@charset "UTF-8";
/* CSS Document */
		td{
			padding-top: 5px;
			padding-bottom: 5px;
			padding-right: 20px;
			padding-left: 20px;
		}
		.wrong {
			background-color: #fa8072;
		}
 
		.right {
    		background-color: transparent;
 		}

Thank you

Yeah that is the correct way to include it. Usually if you find that it is not working it might be because you have a bad URL for the src attribute. The second cause might simply be that the JavaScript is not doing what you think it is doing.

If you put in that script tag, save the page and then reload and right click the page, you can go to the “inspect” option and this will open your developer tools. In the console tab you can see if there are any JavaScript errors being caused. This can tell you what is wrong. If it can’t find the file, it will tell you the loading failed with a 404.

If you are still unsure what the errors mean, you can post them here and I am sure someone can assist you further.

:slight_smile:

1 Like

Thank you for answering.

I looked on the Console and it seems to be attached, i can see all my JavaScript there.

When i have the JavaScript on the botton of my HTML for each question I had the different codes separated.

I had
Question 1 HTML
Question 2 HTML
Question 3 HTML
Then I had

<script> Question 1 JavaScript</script>
<script> Question 2 JavaScript</script>
<script> Question 3 JavaScript</script>

Do I separate the JavaScript in some way like that when its an external JavaScript file?

Well if you are linking to external files you should be using the src attribute like you showed above. As for the actual JavaScript you write, they can all be in the same tag. You don’t have to split the questions out into their own script tags. Think of it as like a mode. You have HTML mode, but once you open a “script” tag you are in JavaScript mode so you can write all JavaScript in there and call functions from your external file. Then once you close the “script” tag you are back in HTML mode.

<script src="external_script_file.js"></script>
<!-- some HTML code -->
<script>
    // All my javascript that calls the code in external_script_file.js can be here in one tag
</script>

So lets say you three functions defined in the external file. HelloWorld1, HelloWorld2 and HelloWorld3. You can call them all in the same script tag…

<script>
    HelloWorld1();
    HelloWorld2();
    HelloWorld3();
</script>

Just in case the obvious isn’t known about, javascript files don’t have <script> tags in the file. That’s a common trap that many people starting out fall for.

1 Like

Ok so when I transfer my JavaScript Code to an external file do I separate it in any way?

Would I keep the separate answers I separate JavaScrip pages?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.