Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with external JS file.
New Posts  All Forums:Forum Nav:

Help with external JS file.

post #1 of 8
Thread Starter 
Hey - I'm working on an assignment for class and the instructor wants us to make an html page and have some javascript with it. He also wants us to make an external javascript file and insert it into our code. The external file is suppose to be a prompt and I just cannot figure it out (first programming class ever)

Fortunately I had a doctor apt 2 hours away the day they went over this in class and yeah - I'm just not comprehending the examples I've seen.

Here is the external js prompt script that I have to use in my html document.
Code:
<script type="text/javascript">
   function getUserID()
   {
   var id
   id=prompt("Please enter an 8 digit number.","my user id");
   document.write(id + ", is the value you entered.");
   }
</script>


I know that doesn't look correct at all but I don't know why I can't get it to work form the examples lol.

And I must not be inserting the external file into my html file correctly either because I can't even get the examples to work.
Code:
<script  type="text/javascript" src="prjJSIntro.js">
      </script>

You insert that into the
Code:
<head>
right?

I will be very appreciative if I could get some help.



Hmmm - I just tried entering my prompt code directly into my html document and that seemed to work - so there must be some error with the way I am inserting the external file then.

Sorry if I'm not making much sense - It's hard to even sound like I'm making sense to myself.
Edited by Zakel2 - 1/27/12 at 7:45pm
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
post #2 of 8
Thread Starter 
Any help would be great... going on like 5 hours of confusion here : /

I have made a little head way though. Whenever I enter my prompt code directly into my html document it seems to work, but when I try to add the external file in it breaks. What confuses me is I appear to have the command to enter the external file entered in correctly but somewhere it must not be correct because it doesn't work.


SO CONFUSED
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
post #3 of 8
Code:
<script type="text/javascript" src="prjJSIntro.js"></script>
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
Server
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 2600k Intel H67DE PNY Verto 9600GT (Thanks to ihasfip!) 16GB G.Skill Ripjaws 1333Mhz 
Hard DriveOSMonitorPower
(3) Seagate 7200K 1TB (2) Hitachi 7200K.12 1TB Proxmox Samsung 17" Corsair GS600 
Case
Chenbro tower 
  hide details  
Reply
post #4 of 8
Thread Starter 
That's exactly what I have entered into the head of my html document but nothing happens when I load the page : /

Here is my entire document - I cannot find out where I'm going wrong.

Here is my external file I need to use name prjJSIntro.js

Code:
//function to get user id code
   function getUserID()
   {
   var id =""
   //variable for function
   id=prompt("Please enter an 8 digit number.","00145366");
   //command
   document.write(id + ", is the value you entered.");
   }


and here is my html document

Code:
<!DOCTYPE html>
<html>
   <head>
      <title>JSIntro Project</title>
      <script type="text/javascript" src="prjJSIntro.js">
      </script>
      <script type="text/javascript">
      //<![CDATA[

      //Function to display my definition of the CDATA element.
      function displayInfo()
      {
      //Display this information on the screen
      document.write("The CDATA element is used to hide the JavaScript code from validators. We want to incorporate the CDATA element because some characters we use to script with will be returned as errors. These characters will show up as an error because the XHTML cannot recognize the elements used for JavaScript.")
      }
      //]]>
      </script>     
   </head>
   
   <body>
      <h1>Requirements for JSIntro Project.</h1>
      
      <p>I would have to say that one of the most important concepts I observed from last weeks module was to be precise.
      While writing code, there is zero tolerance for errors. This does not mean that the most skilled programmers can go without errors. 
      It just refers to an idea that you will need well developed problem solving skills along with an abstract view of things. 
      Unlike human languages, it is usually simple to interpret simple spelling or grammatical errors. 
      Computers on the other hand, have no ability to interpret small mistakes. They do not have an "abstract thinking" process.
      </p>
      <script>
      displayInfo();
      </script>
   </body> 
</html>

If anyone can see where I'm wrong please let me know - I'm absolutely clueless.
Edited by Zakel2 - 1/27/12 at 8:10pm
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
post #5 of 8
You're missing a semicolon after declaring id. Better yet, you can combine that line with the one below it.
Code:
var id = prompt("Please enter an 8 digit number.","00145366");

Also, you never call getUserID(). You need to call it somewhere in your body within a script element like you did with displayInfo().
post #6 of 8
Thread Starter 
Oh man - thank you so so much. So it is ok to combine

var id="" into var id = prompt("Please enter an 8 digit number." ,"00145366")?
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
post #7 of 8
Some might argue that having them on separate lines makes it easier to read code, like how you had it before
Code:
var id =  "";  //<-- don't forget the semicolon
id = prompt("Please enter an 8 digit number.","00145366");

But most people won't find it hard to read if you did it the way I suggested.


You can even combine all three lines:
Code:
document.write(prompt("Please enter an 8 digit number.","00145366") + ",  is the value you entered.");

But this makes it really hard to read and I'm sure your instructor would frown upon it.
post #8 of 8
Thread Starter 
Ahh yes, he wants things as neat as could be. Anyway thanks for the help - I didn't know you had to call upon a function that was in a separate file but now it makes sense. Man - no way can I miss a day of this class.
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with external JS file.