Thursday, 29 February 2024

Applying JavaScript (Internal and External)

 

Applying JavaScript (Internal and External)

JavaScript can be added to your HTML file in two ways:

·      InternalJavaScript

·      ExternalJavaScript

 

Internal JavaScript:

We can add JS code directly to our HTML file by writing the code inside the <script>& </script>. The<script>tag can either be placed inside the<head>orthe<body>tag according to the requirement.

 

Example:

It is the basic example of using JavaScript code inside of HTML code, that script enclosing section can be placed in the body or head of the HTMLdocument.

 

<!DOCTYPEhtml>

<htmllang="en">

 

<head>

<title>

BasicExampletoDescribeJavaScript

</title>

</head>

<script src="main.js"></script>

</head>

<body>

</body>

</html>

 

OUTPUT:

The output will display on console.

Welcome to JavaScript

 

(OR)

b) JavaScript

/* JavaScript code can be embedded inside

head section or body section */

console.log("Welcome to JavaScript");

// JavaScript file name: main.js

 

OUTPUT:

The output will display on console.

Welcome to JavaScript

PROGRAM:

AIM: Creating and Using external JavaScript file in HTML document

// JavaScript Document a=prompt("Enter your Name:");

document.write("<font face='britanic bold' color='green' size=14>Hai..."+a+"</font>");

 

<html>

//accessing external script file from HTML document

<head><title> Using external Java Script</title></head>

<body>

<script language="JavaScript" src="wish.js" type="Text/JavaScript"></script>

</body>

</html>

Introduction to JavaScript

 

Introduction to JavaScript

JavaScript (JS) is a light-weight object-oriented programming language which is used by several websites for scripting the web pages.It is an interpreted, full-fledged programming language that enables dynamic interactivity on websites when applied to an HTML document.

·       JavaScript is also known as a scripting language for webpages.

· in May 1995, Marc Andreessen coined the first code of Javascript named'Mocha'.Later,themarketingteamreplacedthenamewith'LiveScript'.

·       Netscape decided to give a similar name to the scripting language as Java's. It led to'Javascript'.

·       But, due to trademark reasons and certain other reasons, in December 1995,the language was finally renamed to 'JavaScript'.

Why Study JavaScript?

·       JavaScript is one of the 3 languages all web developers must learn:

·              1.HTMLtodefinethecontentofwebpages

·              2.CSStospecifythelayoutofwebpages

·              3.JavaScripttoprogram the behavior of webpages

 

Example:

<script>

document.write("Hello Java Script by JavaScript");

</script>

 

OUTPUT:

Hello Java Script by JavaScript

 

PROGRAM:

 

AIM: To Display Hello World…! On web page using Java Script in colors with specific font

<html>

<head><title>My First Java Script</title>

<script language="JavaScript" type="Text/JavaScript">

<!--

document.write("<font face='bookman old style' size=10 color='orange'>");

document.write("Hello World...!");

document.write("</font>");

document.write("<br><font face='bookman old style' size=10 color='lightgray'>");

document.write("Hello World...!");

document.write("</font>");

document.write("<br><font face='bookman old style' size=10 color='green'>");

document.write("Hello World...!");

document.write("</font>");

//-->

</script>

</head>

<body>

</body>

</html>

Specify Files by Name "Absolute Paths and Relative Paths"

 

Specify Files by Name

Objectives

Specify the absolute location and relative location of files to the current working directory, determine and change the working directory, and list the contents of directories.

Absolute Paths and Relative Paths

The path of a file or directory specifies its unique file-system location. Following a file path traverses one or more named subdirectories, which are delimited by a forward slash (/), until the destination is reached. Directories, also called folders, can contain other files and other subdirectories. Directories are referenced in the same manner as files.

Important

A space character is acceptable in a Linux file name. The shell also uses spaces to distinguish options and arguments on the command line. If a command includes a file with a space in its name, then the shell can misinterpret the command and assume that the file name is multiple arguments. To avoid this mistake, surround such file names in quotation marks so that the shell interprets the name as a single argument. Red Hat recommends avoiding spaces at all in file names.

Absolute Paths

An absolute path is a fully qualified name that specifies the exact location of the file in the file-system hierarchy. The absolute path begins at the root (/) directory and includes each subdirectory that must be traversed to reach the specific file. Every file in a file system has a unique absolute path name, which is recognized with a simple rule: a path name with a forward slash (/) as the first character is an absolute path name.

For example, the absolute path name for the system message log file is /var/log/messages. Absolute path names can be long to type, so files can also be located relative to the current working directory of your shell prompt.

The Current Working Directory and Relative Paths

When a user logs in and opens a command window, the initial location is typically the user's home directory. System processes also have an initial directory. Users and processes change to other directories as needed. The working directory and current working directory terms refer to their current location.

Similar to an absolute path, a relative path identifies a unique location, and specifies only the necessary path to reach the location from the working directory. Relative path names follow this rule: a path name with anything other than a forward slash as the first character is a relative path name. For example, relative to the /var directory, the message log file is log/messages.

Linux file systems, including ext4XFSGFS2, and GlusterFS, are case-sensitive. Creating the FileCase.txt and filecase.txt files in the same directory results in two unique files.

Non-Linux file systems might work differently. For example, VFAT, Microsoft NTFS, and Apple HFS+ have case-preserving behavior. Although these file systems are not case-sensitive, they do display file names with the file's original capitalization. By creating the files in the preceding example on a VFAT file system, both names would point to the same file instead of to two different files.

Navigate Paths in the File System

The pwd command displays the full path name of the current working directory for that shell. This command helps you to determine the syntax to reach files by using relative path names. The ls command lists directory contents for the specified directory or, if no directory is given, for the current working directory.

[user@host ~]$ pwd
/home/user
[user@host ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[user@host ~]$

Use the cd command to change your shell's current working directory. If you do not specify any arguments to the command, then it changes to your home directory.

In the following example, a mixture of absolute and relative paths are used with the cd command to change the current working directory for the shell.

[user@host ~]$ pwd
/home/user
[user@host ~]$ cd Videos
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd /home/user/Documents
[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ cd
[user@host ~]$ pwd
/home/user
[user@host ~]$

In the preceding example, the default shell prompt also displays the last component of the absolute path to the current working directory. For example, for the /home/user/Videos directory, only the Videos directory is displayed. The prompt displays the tilde character (~) when your current working directory is your home directory.

The touch command updates the time stamp of a file to the current date and time without otherwise modifying it. This command is useful for creating empty files, and can be used for practice, because when you use the touch command with a file name that does not exist, the file is created. In the following example, the touch command creates practice files in the Documents and Videos subdirectories.

[user@host ~]$ touch Videos/blockbuster1.ogg
[user@host ~]$ touch Videos/blockbuster2.ogg
[user@host ~]$ touch Documents/thesis_chapter1.odf
[user@host ~]$ touch Documents/thesis_chapter2.odf
[user@host ~]$

The ls command has multiple options for displaying attributes on files. The most common options are -l (long listing format), -a (all files, including hidden files), and -R (recursive, to include the contents of all subdirectories).

[user@host ~]$ ls -l
total 0
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Desktop
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Documents
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Downloads
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Music
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Pictures
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Public
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Templates
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Videos
[user@host ~]$ ls -la
total 40
drwx------. 17 user user 4096 Mar  2 03:07 .
drwxr-xr-x.  4 root root   35 Feb 10 10:48 ..
drwxr-xr-x.  4 user user   27 Mar  2 03:01 .ansible
-rw-------.  1 user user  444 Mar  2 04:32 .bash_history
-rw-r--r--.  1 user user   18 Aug  9  2021 .bash_logout
-rw-r--r--.  1 user user  141 Aug  9  2021 .bash_profile
-rw-r--r--.  1 user user  492 Aug  9  2021 .bashrc
drwxr-xr-x.  9 user user 4096 Mar  2 02:45 .cache
drwxr-xr-x.  9 user user 4096 Mar  2 04:32 .config
drwxr-xr-x.  2 user user    6 Mar  2 02:45 Desktop
drwxr-xr-x.  2 user user    6 Mar  2 02:45 Documents
...output omitted...

At the top of the listing are two special directories. One dot (.) refers to the current directory, and two dots (..) refer to the parent directory. These special directories exist in every directory on the system, and they are useful when using file management commands.

Important

File names that begin with a dot (.) indicate hidden files; you cannot see them in the normal view with ls and other commands. This behavior is not a security feature. Hidden files keep necessary user configuration files from cluttering home directories. Many commands process hidden files only with specific command-line options, and prevent one user's configuration from being accidentally copied to other directories or users.

To protect file contents from improper viewing requires the use of file permissions.

You can also use the tilde (~) special character in combination with other commands for better interaction with the home directory.

[user@host ~]$ cd /var/log/
[user@host log]$ ls -l ~
total 0
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Desktop
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Documents
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Downloads
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Music
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Pictures
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Public
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Templates
drwxr-xr-x. 2 user user 6 Mar  2 02:45 Videos
[user@host ~]$

The cd command has many options. Some options are useful to practice early and to use often. The cd - command changes to the previous directory, where the user was previously to the current directory. The following example illustrates this behavior, and alternates between two directories, which is useful when processing a series of similar tasks.

[user@host ~]$ cd Videos
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd /home/user/Documents
[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ cd -
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd -
[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ cd -
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd
[user@host ~]$

The cd .. command uses the (..) hidden directory to move up one level to the parent directory, without needing to know the exact parent name. The other hidden directory (.) specifies the current directory on commands where the current location is either the source or destination argument, and avoids the need to type the directory's absolute path name.

[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd .
[user@host Videos]$ pwd
/home/user/Videos
[user@host Videos]$ cd ..
[user@host ~]$ pwd
/home/user
[user@host ~]$ cd ..
[user@host home]$ pwd
/home
[user@host home]$ cd ..
[user@host /]$ pwd
/
[user@host /]$ cd
[user@host ~]$ pwd
/home/user
[user@host ~]$

 

References

info libc 'file name resolution' (GNU C Library Reference Manual)

  • Section 11.2.2 File Name Resolution

https://www.gnu.org/software/libc/manual/html_node/File-Name-Resolution.html

bash(1), cd(1), ls(1), pwd(1), unicode(7), and utf-8(7) man pages

UTF-8 and Unicode

Manage Files with Command-line Tools

 

Manage Files with Command-line Tools

Objectives

Create, copy, move, and remove files and directories.

Command-line File Management

Creating, copying, moving, and, removing files and directories are common operations for a system administrator. Without options, some commands are used to interact with files, or they can manipulate directories with the appropriate set of options.

Be aware of the options that are used when running a command. The meaning of some options might differ between commands.

Create Directories

The mkdir command creates one or more directories or subdirectories. It takes as an argument a list of paths to the directories that you want to create.

In the following example, files and directories are organized beneath the /home/user/Documents directory. Use the mkdir command and a space-delimited list of the directory names to create multiple directories.

[user@host ~]$ cd Documents
[user@host Documents]$ mkdir ProjectX ProjectY ProjectZ
[user@host Documents]$ ls
ProjectX  ProjectY ProjectZ

If the directory exists, or a parent directory of the directory that you are trying to create does not exist, then the mkdir command fails and it displays an error.

The mkdir command -p (parent) option creates any missing parent directories for the requested destination. In the following example, the mkdir command creates three ChapterN subdirectories with one command. The -p option creates the missing Thesis parent directory.

[user@host Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
[user@host Documents]$ ls -R Thesis/
Thesis/:
Chapter1  Chapter2  Chapter3

Thesis/Chapter1:

Thesis/Chapter2:

Thesis/Chapter3:

Use the mkdir command -p option with caution, because spelling mistakes can create unintended directories without generating error messages. In the following example, imagine that you are trying to create a Watched subdirectory in the Videos directory, but you accidentally omitted the letter "s" in Videos in your mkdir command.

[user@host ~]$ mkdir Video/Watched
mkdir: cannot create directory Video/Watched: No such file or directory

The mkdir command fails because the Video directory does not exist. If you had used the mkdir command with the -p option, then the Video directory would be created unintentionally. The Watched subdirectory would be created in that incorrect directory.

Copy Files and Directories

The cp command copies a file, and creates a file either in the current directory or in a different specified directory.

[user@host ~]$ cd Videos
[user@host Videos]$ cp blockbuster1.ogg blockbuster3.ogg
[user@host Videos]$ ls -l
total 0
-rw-rw-r--. 1 user user    0 Feb  8 16:23 blockbuster1.ogg
-rw-rw-r--. 1 user user    0 Feb  8 16:24 blockbuster2.ogg
-rw-rw-r--. 1 user user    0 Feb  8 16:34 blockbuster3.ogg

You can also use the cp command to copy multiple files to a directory. In this scenario, the last argument must be a directory. The copied files retain their original names in the new directory. If a file with the same name exists in the target directory, then the existing file is overwritten.

Note

By default, the cp command does not copy directories; it ignores them.

In the following example, two directories are listed as arguments, the Thesis and ProjectX directories. The last argument, the ProjectX directory, is the target and is valid as a destination. The Thesis argument is ignored by the cp command, because it is intended to be copied and it is a directory.

[user@host Documents]$ cp thesis_chapter1.txt thesis_chapter2.txt Thesis ProjectX
cp: -r not specified; omitting directory 'Thesis'
[user@host Documents]$ ls Thesis ProjectX
ProjectX:
thesis_chapter1.txt  thesis_chapter2.txt

Thesis:
Chapter1  Chapter2  Chapter3

The Thesis directory failed to copy, but the copying of the thesis_chapter1.txt and thesis_chapter2.txt files succeeded.

You can copy directories and their contents by using the cp command -r option. Keep in mind that you can use the . and .. special directories in command combinations. In the following example, the Thesis directory and its contents are copied to the ProjectY directory.

[user@host Documents]$ cd ProjectY
[user@host ProjectY]$ cp -r ../Thesis/ .
[user@host ProjectY]$ ls -lR
.:
total 0
drwxr-xr-x. 5 user user 54 Mar  7 15:08 Thesis

./Thesis:
total 0
drwxr-xr-x. 2 user user 6 Mar  7 15:08 Chapter1
drwxr-xr-x. 2 user user 6 Mar  7 15:08 Chapter2
drwxr-xr-x. 2 user user 6 Mar  7 15:08 Chapter3

./Thesis/Chapter1:
total 0

./Thesis/Chapter2:
total 0

./Thesis/Chapter3:
total 0

Move Files and Directories

The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, then moving a file is effectively the same as renaming a file. The contents of the files that are moved remain unchanged.

Use the mv command to rename a file. In the following example, the mv thesis_chapter2.txt command renames the thesis_chapter2.txt file to thesis_chapter2_reviewed.txt in the same directory.

[user@host Documents]$ ls -l
-rw-r--r--. 1 user user  7100 Mar  7 14:37 thesis_chapter1.txt
-rw-r--r--. 1 user user 11431 Mar  7 14:39 thesis_chapter2.txt
...output omitted...
[user@host Documents]$ mv thesis_chapter2.txt thesis_chapter2_reviewed.txt
[user@host Documents]$ ls -l
-rw-r--r--. 1 user user  7100 Mar  7 14:37 thesis_chapter1.txt
-rw-r--r--. 1 user user 11431 Mar  7 14:39 thesis_chapter2_reviewed.txt
...output omitted...

Use the mv command to move a file to a different directory. In the next example, the thesis_chapter1.txt file is moved from the ~/Documents directory to the ~/Documents/Thesis/Chapter1 directory. You can use the mv command -v option to display a detailed output of the command operations.

[user@host Documents]$ ls Thesis/Chapter1
[user@host Documents]$
[user@host Documents]$ mv -v thesis_chapter1.txt Thesis/Chapter1
renamed 'thesis_chapter1.txt' -> 'Thesis/Chapter1/thesis_chapter1.txt'
[user@host Documents]$ ls Thesis/Chapter1
thesis_chapter1.txt
[user@host Documents]$ ls -l
-rw-r--r--. 1 user user 11431 Mar  7 14:39 thesis_chapter2_reviewed.txt
...output omitted...

Remove Files and Directories

The rm command removes files. By default, rm does not remove directories. You can use the rm command -r or the --recursive option to enable the rm command to remove directories and their contents. The rm -r command traverses each subdirectory first, and individually removes their files before removing each directory.

In the following example, the rm command removes the thesis_chapter1.txt file without options, but to remove the Thesis/Chapter1 directory, you must add the -r option.

[user@host Documents]$ ls -l Thesis/Chapter1
-rw-r--r--. 1 user user 7100 Mar  7 14:37 thesis_chapter1.txt
[user@host Documents]$ rm Thesis/Chapter1/thesis_chapter1.txt
[user@host Documents]$ rm Thesis/Chapter1
rm: cannot remove 'Thesis/Chapter1': Is a directory
[user@host Documents]$ rm -r Thesis/Chapter1
[user@host Documents]$ ls -l Thesis
drwxr-xr-x. 2 user user 6 Mar  7 12:37 Chapter2
drwxr-xr-x. 2 user user 6 Mar  7 12:37 Chapter3

Important

Red Hat Enterprise Linux does not provide a command-line undelete feature, nor a "trash bin" from which you can restore files held for deletion. A trash bin is a component of a desktop environment such as GNOME, but is not used by commands that are run from a shell.

It is a good idea to verify your current working directory before you remove a file or directory by using relative paths.

[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ ls -l thesis*
-rw-r--r--. 1 user user 11431 Mar  7 14:39 thesis_chapter2_reviewed.txt
[user@host Documents]$ rm thesis_chapter2_reviewed.txt
[user@host Documents]$ ls -l thesis*
ls: cannot access 'thesis*': No such file or directory

You can use the rm command -i option to interactively prompt for confirmation before deleting. This option is essentially the opposite of using the rm command -f option, which forces the removal without prompting the user for confirmation.

[user@host Documents]$ rm -ri Thesis
rm: descend into directory 'Thesis'? y
rm: descend into directory 'Thesis/Chapter2'? y
rm: remove regular empty file 'Thesis/Chapter2/thesis_chapter2.txt'? y
rm: remove directory 'Thesis/Chapter2'? y
rm: remove directory 'Thesis/Chapter3'? y
rm: remove directory 'Thesis'? y

Warning

If you specify both the -i and -f options, then the -f option takes priority and you are not prompted for confirmation before rm removes files.

You can also use the rmdir command to remove empty directories. Use the rm command -r option to remove non-empty directories.

[user@host Documents]$ pwd
/home/user/Documents
[user@host Documents]$ rmdir ProjectZ
[user@host Documents]$ rmdir ProjectX
rmdir: failed to remove 'ProjectX': Directory not empty
[user@host Documents]$ rm -r ProjectX
[user@host Documents]$

 

References

cp(1), mkdir(1), mv(1), rm(1), and rmdir(1) man pages