How to write a js file




















JavaScript is a simple yet very powerful scripting language. Why not use your knowledge of JavaScript for batch processing of local files and other common tasks? Well, you can! Not through the Internet of course, but internally on your computer, or on your intranet if you have one. How can you use JavaScript to access your local files and folders? Currently there are two ways to do it:. Using a web page and ActiveX objects Internet Explorer only. Run JavaScript Editor 2. If you have trouble opening the Zip file, the free 7-Zip utility can help: www.

On a Mac, simply double-click the file to decompress it. To get your feet wet and provide a gentle introduction to JavaScript, your first program will be very simple:. In your favorite text editor, open the file hello. It informs the web browser that the stuff following this tag is JavaScript. Press the Return key to create a new blank line, and type :. The JavaScript alert function is a command that pops open an Alert box and displays the message that appears inside the parentheses—in this case, hello world.

The code should now look like this :. In this example, the stuff you just typed is shown in boldface. The two HTML tags are already in the file; make sure you type the code exactly where shown. Launch a web browser and open the hello. A JavaScript Alert box appears see Figure Notice that the page is blank when the alert appears. Double-check your typing and read the following Tip.

For new programmers, the most common cause of nonfunctioning programs is simple typing mistakes. Always double-check to make sure you spelled commands like alert in the first script correctly.

Also, notice that punctuation frequently comes in pairs the opening and closing parentheses, and single-quote marks from your first script, for example. So when you try to preview the tutorial files for this book in Internet Explorer, you might see a message saying that IE has blocked the script.

This annoying behavior only applies to web pages you preview from your computer, not to files you put up on a web server. The script in the previous section popped up a dialog box in the middle of your monitor. What if you want to print a message directly onto a web page using JavaScript? In your text editor, open the file hello2. Like the alert function, document. The tutorial files you downloaded also include the completed version of each tutorial. The two scripts you just created may leave you feeling a little underwhelmed with JavaScript…or this book.

You then instruct your web pages to load that file and use the JavaScript inside it. In particular, there are collections of JavaScript code called libraries , which provide useful JavaScript programming. In your text editor, open the file fadeIn. This code links a file named jquery. When a web browser loads this web page, it also downloads the jquery.

The min part means that the file is minimized —a process that removes unneeded whitespace and condenses the code to make the file smaller so that it downloads faster. Press Return to create a new blank line, and then type :. HTML tags usually travel in pairs—an opening and closing tag. Press Return twice to create two blank lines, and then type :. Click the empty line between the opening and closing script tags and type :.

How does it do that? One typo, and the program may not work. Much as you indent HTML tags to show which tags are nested inside of other tags, you can indent lines of code that are inside another block of code. For example, the line of code you added in step 6 is nested inside the code for steps 5 and 7, so hitting Tab or pressing the spacebar a couple of times before typing the code for step 6 can make your code easier to understand as pictured in the final code listed at the end of step 7.

Change the number to different values like and to see how that changes the way the page works. Chapters Chapter 2 and Chapter 3 will cover the basics of JavaScript to get you comfortable with the fundamental concepts and syntax that make up the language.

The most frustrating moment in JavaScript programming comes when you try to view your JavaScript-powered page in a web browser…and nothing happens. There are many ways to track errors in a JavaScript program.

Most web browsers keep track of JavaScript errors and record them in a separate window called an error console. When you load a web page that contains an error, you can then view the console to get helpful information about the error, like which line of the web page it occurred in and a description of the error. Often, you can find the answer to the problem in the error console, fix the JavaScript, and then the page will work. You would then save the file with a. For instance, you could save it as write.

Once the file is saved, you can call it from the HTML code via the src attribute of the opening script tag, as shown below for write. The procedure above has the same effect as writing the code between the script tags, but won't clutter the HTML code with JavaScript.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Now at it's lowest price ever! Previous Javascript Program to read text File. Next Understanding basic JavaScript codes. Recommended Articles. How to write a program that takes a predicate and array, like Array. How to write a program that will return true if the bottom of the page is visible in JavaScript?



0コメント

  • 1000 / 1000