Showing posts with label Learn JavaScript. Show all posts
Showing posts with label Learn JavaScript. Show all posts
Wednesday 26 November 2014
Wednesday 19 March 2014
Jscript Function Statement | How To Write Jscript Functions
Jscript Function Statement, Jscript Functions Vs Jscript
Functions, Jscript Functions Example, Jscript Blog.
In order to create a JScript, you
only need to open your text editor, create a plain text file, add a few JScript
statements to it, and then save it with a .js file extension. For example,
create a new JScript called my_first_prog.js and add the following statements
to it.
//Script 1.5 - Your first JScript
WScript.Echo("Well To ProgrammingDost!");
The first statement is a comment
that was embedded within the file to document the script's name and purpose.
The second statement displays a text message. The composition of the second
statement is as follows. First the WScript object is referenced. This object is
different from the WScript execution host. Then a method called Echo, which
belongs to the WScript object, is executed and passed the text string "Well
To ProgrammingDost!". Don't worry if this all sounds a little confusing or
overwhelming now. By the time that you get through Sunday evening, you'll
understand how this stuff works. For now, just follow along and focus on the
steps involved in creating and executing your first JScript.
When executed by either the WScript
or CScript execution host, this JScript displays its message. However,
depending on which execution host you choose to use when running the script, the
results will vary significantly.
If you were to run the scripts directly from the
Windows desktop by double-clicking on it, Windows would, by default, run it
using the WScript execution host. As a result, the script's output would be
displayed in the form of a graphical pop-up dialog.
NOTE
You can also use the WScript execution host to run scripts
from the Windows command prompt,in which case the script will run exactly as if
you had started it from the Windows desktop.
Even some of the most experienced computer users have limited
experience working with the Windows command prompt and little if any
understanding of the Windows commands that are available. However, if you plan
on becoming a serious JScript programmer, you need to get comfortable working
with the Windows shell.
The Windows shell provides a text-based interface between the
user and the operating system. Instead of using the computer's mouse to point
and click your way through the Windows graphical user interface, you work with
the Windows shell by typing in text commands. The Windows shell translates
these commands into a format the operating system can understand. Likewise, the
Windows shell translates any output returned by the operating system into a
format that people can understand.
You enter commands for the Windows shell to process
by typing them in at the Windows command prompt. For example, on a computer
running Windows XP, you would start up a new Windows shell session by clicking
on Start, All Programs, Accessories, and then Command Prompt.
TIP
As a shortcut, you can start up a new Windows shell session
and access the Windows command prompt by clicking on Start, Run, typing CMD,
and then clicking on OK.
NOTE
If your script resides in a folder other than the folder upon
which the Windows shell is currently focused, you must supply the complete path
to the folder where the script is stored. For example, if your script were
stored in C:\Jscripts, then you
would need to use the following command to run it using the WScript execution
host.
TIP
If you are faster at the keyboard than you are with your mouse
then you can also close the Command Prompt windows by typing EXIT and pressing the Enter key.
Jscript Introduction | What is Jscript
What Is A Jscript File, What Is A Jscript Script File,
Jscript Tutorial, Jscript Examples, Jscript Download,
Jscript Array, Jscript Alert.
Jscript and VBScript are the two
default scripting languages which are supplied by the Microsoft to use with the
WSH (Windows Script Host). Both the languages are pretty much similar to each
other. These are very useful when it comes to the automating small tasks.
As you know that the VBScript is
derived from Visual Basic programming language. Like JavaScript,VBScript can
also be used for Web page development. However, while Internet Explorer
supports VBScript, it does not work with some browsers like in past with
Netscape. This makes VBScript a less universally accepted scripting language.
In addition, VBScript is proprietary. This means that Microsoft owns it,
whereas the current version of JavaScript and JScript are based on the
ECMAScript standards, so no one company controls its future. As a result, many
people prefer JavaScript and JScript to VBScript.
There are some easy function in
Jscript and JavaScript to solve complex calculations. But you will find a
better way in VBScript to work with the arrays. People with a JavaScript
background will be able to put together the move to JScript with very little
effort. Similarly, people with a strong Visual Basic programming background may
prefer learning VBScript. Some other third-party scripting languages are
available that also can be used with the WSH. These languages include PERL,
Python and REXX.
The main reason for using JScript and the WSH (Windows Script Host) is to save you time by
automating system tasks. At the heart of every one of Microsoft's Windows
operating systems is an intuitive graphical user
interface (GUI). This GUI is designed to make the computer easy to use.
All that you have to do is grab on to the mouse and point and click your way
around.
Unfortunately, the GUI is not well suited to many tasks,
particularly those that are highly repetitious or that involve many steps. For
example, suppose that you are responsible for defining new user accounts on a
shared computer in your department at work, and one day your boss comes in with
a floppy disk containing a file with a list of 100 newly hired employees.
Using the GUI, you'd probably spend hours in front of the
computer defining new user accounts, during which time no one else could use
the computer to get any work done. On the other hand, you could write JScript
and use the WSH to run it in less than an hour. This script could even be
designed to read the file provided by your boss and to create a new user
account for each name stored in the list. Once executed, the script would
create the new user accounts within seconds. Best of all, once it is written,
you could return and run the script again in the future with no additional work
on your part to create new user accounts.
What Is Wsh | What Is Windows Script Host
JavaScript runs in browser with HTML support. What Is Wsh, What Is Wsh Act, What Is
Wscript, Windows Script Host,
Windows Script Host Reference, Wsh
Script Example.
The WSH is an optional software element
that supports the execution of scripts on Windows operating systems. It is
tightly coupled with the operating system, meaning that it provides direct
access to a number of Windows resources, including these:
Ø The Windows desktop and Start menu
Ø The Windows Quick Launch Bar
Ø The Windows file system
Ø Local and network printers and drives
Ø Windows applications
Ø Windows services
Ø User accounts
Ø The Windows registry
The WSH gives the most complete
scripting environment available today for all Windows operating systems. When
combined with a scripting language such as JScript, it provides a perfect
platform for developing scripts that can automate virtually any Windows task.
Regardless
of which of the Microsoft operating systems you own or support, you will want
to make sure that you have upgraded the version of the WSH that is installed to
the most current version. Microsoft provides access to the most current version
of the WSH through its official scripting Web site,
msdn.microsoft.com/scripting, where you can download WSH for free.
A Brief Overview of the WSH Architecture
The WSH provides built-in support for both JScript and
VBScript. In addition, it can support any third-party, WSH-compatible scripting
language, including PERL, Python, and REXX. In the future, third-party software
developers may provide other WSH-compatible scripting languages. Microsoft
designed the WSH to be very extensible, as evidenced by the number of
third-party scripting languages that has already been set up to work with it.
However, because JScript and VBScript are supplied with the WSH, they are the
most commonly used scripting languages.
The WSH is a 32-bit application. Its architecture consists of
three separate layers. First there are the various WSH-supported scripting
engines. These scripting engines interpret script statements and pass them on
to an execution host for processing.
The WSH provides two different execution hosts. The WScript
execution host is designed to support the execution of scripts directly from
the Windows desktop. The CScript execution host, on the other hand, is designed
to support the execution of scripts from the Windows command prompt. Each of
these execution hosts exists as a separate executable file named Wscript.exe and Cscript.exe, respectively.
With one exception, the WScript and CScript execution hosts
provide the same level of functionality. This exception is the WScript
execution host's capability to enable scripts to display text messages and
collection input from users using graphical pop-up dialogs. This makes the
WScript execution host the proper choice for scripts that need to interact
directly with users. The CScript execution host, on the other hand, generally
is used to run scripts that do not interact with users or to run scripts for users
who are comfortable working from the Windows command prompt. However, the
WScript execution host can be used to run scripts from the command line as
well. Unless your script is designed to generate graphical pop-up dialogs, the
choice of which script execution host to use is completely arbitrary.
The purpose of the WSH core object model is to provide access
to Windows resources. It accomplishes this by defining objects that represent
actual resources. These objects can then be accessed and manipulated by
scripts, thus allowing the scripts to access and manipulate the resources that
the objects represent. Examples of the types of objects that the WSH makes
available include objects that represent Windows shortcuts, files, folders, and
disk drives. The WSH also provides objects that represent network resources
such as network drives and printers. On top of all this, the WSH object model
also provides access to system resources such as Windows events logs and the
Windows registry.
Every object that is presented by the WSH is associated with a
collection of properties and methods. For example, a file is an example of an
object. File objects have file names and file extensions. By accessing these
file object properties, your JScripts can locate files that they want to work
with. In addition, by modifying file object properties, your JScripts can
rename files and change their file types.
Sunday 27 October 2013
Include Javascript File in Html
Ways of Embedding and Integrating JavaScript in the HTML Pages
(js tag, javascript embed tag, embed javascript into html, include javascript file in html, javascript within html, include javascript file, include js file in html, add js file to html)
As you know, there are two places
you can put your JavaScripts in an HTML page: in either the head or body
section. In addition, I have told you that you can either embed JavaScript
directly into the HTML page or reference it in as an external .js file. One
more way you can integrate JavaScript into an HTML page is as a component in an
HTML tag.
As we already told you that we can
put our JavaScript code in anywhere in <head> or in <body> tags. We
told you that you can embed your code directly or in your code or by saving it
in another file and then referring the web page to get data from that external
file. Here we are going to define how to use both ways of embedding code in an
HTML page.
Using or Embedding JavaScript Code in the Body Section of an HTML page
JavaScripts embedded with the <SCRIPT>
and </SCRIPT> tags can be placed anywhere in the body section of an HTML
page. Scripts embedded in the body section are executed as part of the HTML
document when the page loads. This enables the script to begin executing
automatically as the page loads. For example, the statements shown below
demonstrate how to embed a JavaScript within the body section of an HTML page.
<BODY>
<SCRIPT TYPE="Text/JavaScript"
LANGUAGE="JavaScript" >
document.write("Code embedded in the body of an HTML page.");
</SCRIPT>
</BODY>
JavaScript can be used or embedded
more than once in the HTML pages.
<BODY>
<SCRIPT TYPE="Text/JavaScript"
LANGUAGE="JavaScript" >
document.write("First embedded code in the body section.");
</SCRIPT>
<BR>
<SCRIPT
LANGUAGE="JavaScript" TYPE="Text/JavaScript">
document.write("Second embedded code in the body section.");
</SCRIPT>
</BODY>
Embedding the JavaScript in <Head> and </Head> tags in HTML pages
JavaScripts can also be placed anywhere within the head
section of your HTML pages. Unlike scripts embedded within the body section of
HTML pages, scripts embedded in the head section are not necessarily
automatically executed when the page loads. In some cases, they are executed
only when called for execution by other statements within the HTML page. Most
JavaScript programmers move all functions and most variables to the head
section because this ensures that they will be defined before being referenced
by scripts located in the body section of the page.
NOTE
Variables are containers for
storing information in computer memory. Functions
are groups of JavaScript statements that you can call to perform a specific
task. We'll talk more about the benefits of using functions and variables in
upcoming posts.
The following statements show an HTML page with a JavaScript
embedded in the head section. This script will automatically execute when the
HTML page is loaded.
<HTML>
<HEAD>
<TITLE>Programming Dost Home Page</TITLE>
<SCRIPT TYPE="Text/JavaScript" LANGUAGE="JavaScript">
window.alert("JavaScript is executing from the Head section");
</SCRIPT>
</HEAD>
<BODY>
</BODY>
<HTML>
Using JavaScript with an External file in HTML pages
To store your JavaScripts in
external files, you need to save them as plain text files with a .js file
extension. You then need to add the SCR attribute to the opening <SCRIPT>
tag in your HTML page as stated here.
<SCRIPT SRC="myExternalFile.js" TYPE="Text/JavaScript"
LANGUAGE="JavaScript" > </SCRIPT>
In this example, an external
JavaScript named myExternalFile.js has been used. This external JavaScript can
contain any number of JavaScript statements or code. However, it cannot have
any HTML whatsoever. Otherwise you'll end up with an error. For instance, the
contents of the myExternalFile.js script
might be very simple as follows:
document.write("External JavaScript for the friends(dost)
of Programming Dost.");
There are many advantages to putting JavaScripts in externally
referenced files. For beginners, by getting JavaScripts out of your HTML pages
you make your HTML pages smaller and easier to work with. In addition, you can
reuse the JavaScripts stored as external files over and over again by
referencing them from any number of HTML pages. This way if you create a script
that you want to reference from several HTML pages, you can do so without
having to insert the same script in all HTML pages over and over again. As a plus,
should you ever want to alter the functionality of an externally stored script,
you can do it by changing the code in the external file and all is done for
you.
NOTE
There is no limit of the external file which can be called in
an HTML page. And there is not limit of code use in the external file.
Use of JavaScript in the HTML tags in Web Pages
JavaScript can also be placed within
HTML tags, as shown in the following example.
<BODY onLoad=document.write("Hello World! From
Programming Dost")> </BODY>
In this example, the JavaScript onLoad=document.write("Hello
World! From Programming Dost ") statement has been added to the HTML <BODY>
tag. This particular JavaScript statement tells the browser to write the
enclosed text when the browser first loads the HTML page.
JavaScript and Case Sensitivity | JavaScript Case Sensitivity
JavaScript and Case Sensitivity
It should be kept in mind the JavaScript is a case sensitive programming language (Not like the HTML). So you should be extremely careful about the code you embed while using JavaScript. Case sensitivity means that you must type JavaScript elements exactly as they appear in this book in order for them to work. For example, as far as JavaScript is concerned, the words “variable” and “Variable” refer to two different things, so pay special attention when typing your scripts. As for example in our program instead of using document.write() we mistaken and used Document.write() so our program ended with an error. So be very careful with JavaScript’s case sensitivity.Insert JavascriptInto Html
Integrating
JavaScript with HTML
(insert javascript into html, javascript insert html after element, embed jquery in html, embed javascript in php, javascript innerhtml, jquery insert html)
JavaScript is a collection of some
or many programming statements that a programmer embeds in an HTML document.
These statements will work or affect the HTML when they are placed in the
<script> and </script> tags. Placement of these tags is up to the
developer that where he or she wants to put them in the head section or in the
body section.
Previously
the Language was used to specify the
type of the <script> tag. For example:
<script LANGUAGE="JavaScript1.5"
> </script>
According
to the HTML 4.0 specification, the TYPE attribute is now
the proper way to go. However, you can continue to use both attributes if you
want, in order to ensure that older browsers don't get confused. When working
with JavaScripts, the TYPE attribute will always be Text/JavaScript.
Another way to work with JavaScripts is to store them in
external files that have a .js
file extension and then to reference those files from within your HTML pages.
To accomplish this, you use the SRC
attribute to specify the location of an external JavaScript file. This is use full as it makes difficult for the
user to view the JavaScript code. It
also makes it possible to share the same JavaScripts among multiple HTML pages.
Once you have defined the opening and closing tags, you can begin placing
JavaScript statements between them.
Before writing your first program in JavaScript you should
know a little about the structure of the HTML page. If you are not so familiar
with these tags and structure please do check the Programming Dost’s Learning HTML section.
<HTML>
<HEAD>
<TITLE>The
Title of the Page goes here</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>
This HTML page structure contains the <HEAD>
</HEAD>, <TITLE>
</TITLE>, and <BODY>
</BODY> tag sets all
wrapped inside the starting and ending <HTML>
</HTML> tag set. If
you want to do so, create your own template now. When you are done, add the
following lines inside the body section:
<SCRIPT TYPE="Text/JavaScript" LANGUAGE="JavaScript" >
document.write("Hello World From Programming Dost");
</SCRIPT>
After you put the code in the head or body tags the code will
look like:
<HTML>
<HEAD>
<TITLE> The Title of the Page goes here</TITLE>
</HEAD>
<BODY>
<SCRIPT TYPE="Text/JavaScript" LANGUAGE="JavaScript" >
document.write("Hello World From Programming Dost");
</SCRIPT>
</BODY>
</HTML>
How to test your JavaScript Code?
Now that you have typed in your first script, you need to save
it. We called this script as javaScriptTutorial_1.html.
The HTML extension identifies
the page as an HTML page. Your computer uses the information in the file's
extension to associate the file with a particular application. An .html
extension tells the operating system to open its default browser and pass the
HTML file to it. Alternatively, you can use the .htm
extension, which is also recognized as an extension for HTML pages.
If you are using a full-featured HTML editor, the editor may
enable you to test your script with the click of a button like Dreamweaver or
Visual Studio etc. Because Notepad has no such automatic HTML testing feature, we
simply started up a browser and used it to open the javaScriptTutorial_1.html
file. The browser opened our page and ran the script.
Subscribe to:
Posts (Atom)