Home > Web Front-end > JS Tutorial > Foreign JavaScript tutorials for beginners_Basic knowledge

Foreign JavaScript tutorials for beginners_Basic knowledge

WBOY
Release: 2016-05-16 19:03:54
Original
1633 people have browsed it

Embedding and including

Let's first see a simple example:

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span>This is a JavaScript example<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>script</SPAN> <SPAN class=code-attribute>language</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>JavaScript"</SPAN><SPAN class=code-keyword>></span> 
<!-- 
document.write(<SPAN class=code-string>"</SPAN><SPAN class=code-string>Hello World!"</SPAN>); 
<SPAN class=code-comment>//</SPAN><SPAN class=code-comment>--> 
 
<span class="code-keyword"></</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> Hi, man! <span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

Usually, JavaScript code starts with the tag <span class="code-keyword"><</SPAN>script language=<SPAN class=code-string>"</SPAN><SPAN class=code-string>JavaScript"</SPAN><SPAN class=code-keyword>></span> and ends with the tag <span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span>. The code placed between <span class="code-keyword"><</SPAN>head<SPAN class=code-keyword>></span> and <span class="code-keyword"><</SPAN>/head<SPAN class=code-keyword>></span>. Sometimes, people embed the code in the <span class="code-keyword"><</SPAN>body<SPAN class=code-keyword>></span> tags:

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
.....// The code embedded <span class="code-keyword">in</span> the <body> tags. 
<span class="code-keyword"></</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

Why do we place JavaScript code inside comment fields <span class="code-keyword"><!--</SPAN><SPAN class=code-keyword></SPAN> and <SPAN class=code-comment>//</SPAN><SPAN class=code-comment>--></span>? It's for ensuring that the Script is not displayed by old browsers that do not support JavaScript. This is optional, but considered good practice. The LANGUAGE attribute also is optional, but recommended. You may specify a particular version of JavaScript:

<span class="code-keyword"><</SPAN>script language=<SPAN class=code-string>"</SPAN><SPAN class=code-string>JavaScript1.2"</SPAN><SPAN class=code-keyword>></span>
Copy after login

You can use another attribute SRC to include an external file containing JavaScript code:

<span class="code-keyword"><</SPAN>script language=<SPAN class=code-string>"</SPAN><SPAN class=code-string>JavaScript"</SPAN> src=<SPAN class=code-string>"</SPAN><SPAN class=code-string>hello.js"</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span>
Copy after login

For example, shown below is the code of the external file hello.js:

document.write(<span class="code-string">"</span><span class="code-string">Hello World!"</span>)
Copy after login

The external file is simply a text file containing JavaScript code with the file name extension ".js". Note:

  1. Including an external file only functions reliably across platforms in the version 4 browsers.
  2. The code can't include tags <span class="code-keyword"><</SPAN>script language...<SPAN class=code-keyword>></span> and <span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span>, or you will get an error message.

Back to top

write and writeln

In order to output text in JavaScript you must use write() or writeln(). Here's an example:

<span class="code-keyword"><</SPAN>HTML<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>HEAD<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>TITLE<SPAN class=code-keyword>></span> Welcome to my site<span class="code-keyword"><</SPAN>/TITLE<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/HEAD<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>BODY<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>SCRIPT LANGUAGE=<SPAN class=code-string>"</SPAN><SPAN class=code-string>JAVASCRIPT"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>!-- 
document.write(<SPAN class=code-string>"</SPAN><SPAN class=code-string>Welcome to my site!"</SPAN>); 
<SPAN class=code-comment>//</SPAN><SPAN class=code-comment> --> 
</span> 
<span class="code-keyword"><</SPAN>/SCRIPT<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/BODY<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/HTML<SPAN class=code-keyword>></span>
Copy after login

Note: the document object write is in lowercase as JavaScript is case sensitive. The difference between write and writeln is: write just outputs a text, writeln outputs the text and a line break.


Back to top

Document object

The document object is one of the most important objects of JavaScript. Shown below is a very simple JavaScript code:

document.write(<span class="code-string">"</span><span class="code-string">Hi there."</span>)
Copy after login

In this code, document is the object. write is the method of this object. Let's have a look at some of the other methods that the document object possesses.

lastModified

You can always include the last update date on your page by using the following code:
 
<span class="code-keyword"><</SPAN>script language=<SPAN class=code-string>"</SPAN><SPAN class=code-string>JavaScript"</SPAN><SPAN class=code-keyword>></span> 
document.write(<span class="code-string">"</span><span class="code-string">This page created by John N. Last update:"</span> + document.lastModified); 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span> 
Copy after login

All you need to do here is use the lastModified property of the document. Notice that we used + to put together This page created by John N. Last update: and document.lastModified.

bgColor and fgColor

Lets try playing around with bgColor and fgColor:

<span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> 
document.bgColor=<span class="code-string">"</span><span class="code-string">black"</span> 
document.fgColor=<span class="code-string">"</span><span class="code-string">#336699"</span> 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span> 
Copy after login

Back to top

Message Box

alert

There are three message boxes: alert, confirm, and prompt. Let's look at the first one:
<span class="code-keyword"><</SPAN>body<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> 
window.alert(<span class="code-string">"</span><span class="code-string">Welcome to my site!"</span>) 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/body<SPAN class=code-keyword>></span>
Copy after login

You can put whatever you want inside the quotation marks.

confirm

An example for confirm box:
 
window.confirm(<span class="code-string">"</span><span class="code-string">Are you sure you want to quit?"</span>) 
Copy after login

prompt

Prompt box is used to allow a user to enter something according the promotion:

 
window.prompt(<span class="code-string">"</span><span class="code-string">please enter user name"</span>) 
Copy after login

In all our examples above, we wrote the box methods as window.alert(). Actually, we could simply write the following instead as:

alert() 
confirm() 
prompt()
Copy after login

Back to top

Variables and Conditions

Let's see an example:

<span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> 
var x=window.confirm(<span class="code-string">"</span><span class="code-string">Are you sure you want to quit"</span>) 

<span class="code-keyword">if</span> (x) 
  window.alert(<span class="code-string">"</span><span class="code-string">Thank you."</span>) 
<span class="code-keyword">else</span> 
  window.alert(<span class="code-string">"</span><span class="code-string">Good choice."</span>) 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span>
Copy after login

There are several concepts that we should know. First of all, var x= is a variable declaration. If you want to create a variable, you must declare the variable using the var statement. x will get the result, namely, <span class="code-keyword">true</span> or <span class="code-keyword">false</span>. Then we use a condition statement <span class="code-keyword">if</span> <span class="code-keyword">else</span> to give the script the ability to choose between two paths, depending on this result (condition for the following action). If the result is true (the user clicked "ok"), "Thank you" appears in the window box. If the result is false (the user clicked "cancel"), "Good choice" appears in the window box instead. So we can make more complex boxes using var, <span class="code-keyword">if</span> and those basic methods.

<span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> 
var y=window.prompt(<span class="code-string">"</span><span class="code-string">please enter your name"</span>) 
window.alert(y) 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span>
Copy after login

Another example:

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
var x=confirm(<span class="code-string">"</span><span class="code-string">Are you sure you want to quit?"</span>) 
<span class="code-keyword">if</span> (!x) 
  window.location=<span class="code-string">"</span><span class="code-string">http://www.yahoo.com"</span> 
<span class="code-keyword"></</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
Welcome to my website!. 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

If you click "cancel", it will take you to yahoo, and clicking ok will continue with the loading of the current page "Welcome to my website!". Note:<span class="code-keyword">if</span> (!x)means: if click "cancel". In JavaScript, the exclamation mark ! means: "none".


Back to top

Function

Functions are chunks of code.Let's create a simple function:

function test() 
{ 
  document.write(<span class="code-string">"</span><span class="code-string">Hello can you see me?"</span>) 
}
Copy after login

Note that if only this were within your <span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span> tags, you will not see "Hello can you see me?" on your screen because functions are not executed by themselves until you call upon them. So we should do something:

function test() 
{ 
  document.write(<span class="code-string">"</span><span class="code-string">Hello can you see me?"</span>) 
} 
test() 
Copy after login

Last linetest() calls the function, now you will see the words "Hello can you see me?".


Back to top

Event handler

What are event handlers? They can be considered as triggers that execute JavaScript when something happens, such as click or move your mouse over a link, submit a form etc.

onClick

onClick handlers execute something only when users click on buttons, links, etc. Let's see an example:

<span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> 
function ss() 
{ 
alert(<span class="code-string">"</span><span class="code-string">Thank you!"</span>) 
} 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Click here"</SPAN> onclick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>ss()"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span>
Copy after login

The function ss() is invoked when the user clicks the button. Note: Event handlers are not added inside the <span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> tags, but rather, inside the html tags.

onLoad

The onload event handler is used to call the execution of JavaScript after loading:

<span class="code-keyword"><</SPAN>body onload=<SPAN class=code-string>"</SPAN><SPAN class=code-string>ss()"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>frameset onload=<SPAN class=code-string>"</SPAN><SPAN class=code-string>ss()"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>img src=<SPAN class=code-string>"</SPAN><SPAN class=code-string>whatever.gif"</SPAN> onload=<SPAN class=code-string>"</SPAN><SPAN class=code-string>ss()"</SPAN><SPAN class=code-keyword>></span>
Copy after login

onMouseover,onMouseout

These handlers are used exclusively with links.

<span class="code-keyword"><</SPAN>a href=<SPAN class=code-string>"</SPAN><SPAN class=code-string>#"</SPAN> onMouseOver=<SPAN class=code-string>"</SPAN><SPAN class=code-string>document.write('Hi, nice to see you!"</SPAN><SPAN class=code-keyword>></span>Over Here!<span class="code-keyword"><</SPAN>/a<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>a href=<SPAN class=code-string>"</SPAN><SPAN class=code-string>#"</SPAN> onMouseOut=<SPAN class=code-string>"</SPAN><SPAN class=code-string>alert('Good try!')"</SPAN><SPAN class=code-keyword>></span>Get Out Here!<span class="code-keyword"><</SPAN>/a<SPAN class=code-keyword>></span>
Copy after login

onUnload

onunload executes JavaScript while someone leaves the page. For example to thank users.

<span class="code-keyword"><</SPAN>body onunload=<SPAN class=code-string>"</SPAN><SPAN class=code-string>alert('Thank you for visiting us. See you soon')"</SPAN><SPAN class=code-keyword>></span>
Copy after login

Handle multiple actions

How do you have an event handler call multiple functions/statements? That's simple. You just need to embed the functions inside the event handler as usual, but separate each of them using a semicolon:

<span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Click here!"</SPAN> onClick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>alert('Thanks 
for visiting my site!');window.location='http://www.yahoo.com'"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span>
Copy after login

Back to top

Form

Let's say you have a form like this:

<span class="code-keyword"><</SPAN>form name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>aa"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>text"</SPAN> size=<SPAN class=code-string>"</SPAN><SPAN class=code-string>10"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>"</SPAN> name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>bb"</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>br<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> 
value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Click Here"</SPAN>onclick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>alert(document.aa.bb.value)"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span>
Copy after login

Notice that we gave the names to the form and the element. So JavaScript can gain access to them.

onBlur

If you want to get information from users and want to check each element (ie: user name, password, email) individually, and alert the user to correct the wrong input before moving on, you can use onBlur. Let's see how onblur works:

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword">function</span> emailchk() 
{ 
var x=document.feedback.email.value 
<span class="code-keyword">if</span> (x.indexOf(<span class="code-string">"</span><span class="code-string">@"</span>)==-1) 
{ 
  alert(<span class="code-string">"</span><span class="code-string">It seems you entered an invalid email address."</span>) 
  document.feedback.email.focus() 
} 
} 
<span class="code-keyword"></</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span>
Copy after login
<span class="code-keyword"><</SPAN>form 
name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>feedback"</SPAN><SPAN class=code-keyword>></span> 
Email:<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>text"</SPAN> size=<SPAN class=code-string>"</SPAN><SPAN class=code-string>20"</SPAN> name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>email"</SPAN> 
onblur=<SPAN class=code-string>"</SPAN><SPAN class=code-string>emailchk()"</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>br<SPAN class=code-keyword>></span> 
Comment: <span class="code-keyword"><</SPAN>textarea name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>comment"</SPAN> rows=<SPAN class=code-string>"</SPAN><SPAN class=code-string>2"</SPAN> cols=<SPAN class=code-string>"</SPAN><SPAN class=code-string>20"</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/textarea<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>br<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>submit"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Submit"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/body<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/html<SPAN class=code-keyword>></span>
Copy after login

If you enter an email address without the @, you'll get an alert asking you to re-enter the data. What is: x.indexOf(@)==-<span class="code-digit">1</span>? This is a method that JavaScript can search every character within a string and look for what we want. If it finds it will return the position of the char within the string. If it doesn't, it will return -1. Therefore, x.indexOf(<span class="code-string">"</span><span class="code-string">@"</span>)==-<span class="code-digit">1</span> basically means: "if the string doesn't include @, then:

alert(<span class="code-string">"</span><span class="code-string">It seems you entered an invalid email address."</span>) 
document.feedback.email.focus()
Copy after login

What's focus()? This is a method of the text box, which basically forces the cursor to be at the specified text box. onsubmit Unlike onblur, onsubmit handler is inserted inside the <span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> tag, and not inside any one element. Lets do an example:

<span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>!-- 
function validate() 
{ 
<SPAN class=code-keyword>if</SPAN>(document.login.userName.value==<SPAN class=code-string>"</SPAN><SPAN class=code-string>"</SPAN>) 
{ 
  alert (<SPAN class=code-string>"</SPAN><SPAN class=code-string>Please enter User Name"</SPAN>) 
  <SPAN class=code-keyword>return</SPAN> <SPAN class=code-keyword>false</SPAN> 
} 
<SPAN class=code-keyword>if</SPAN>(document.login.password.value==<SPAN class=code-string>"</SPAN><SPAN class=code-string>"</SPAN>) 
{ 
  alert (<SPAN class=code-string>"</SPAN><SPAN class=code-string>Please enter Password"</SPAN>) 
  <SPAN class=code-keyword>return</SPAN> <SPAN class=code-keyword>false</SPAN> 
} 
} 
<SPAN class=code-comment>//</SPAN><SPAN class=code-comment>--> 
</span> 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span>
Copy after login
<span class="code-keyword"><</SPAN>form name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>login"</SPAN> onsubmit=<SPAN class=code-string>"</SPAN><SPAN class=code-string>return validate()"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>text"</SPAN> size=<SPAN class=code-string>"</SPAN><SPAN class=code-string>20"</SPAN> name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>userName"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>text"</SPAN> size=<SPAN class=code-string>"</SPAN><SPAN class=code-string>20"</SPAN> name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>password"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>submit"</SPAN> name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>submit"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Submit"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span>
Copy after login

Note:
<span class="code-keyword">if</span>(document.login.userName.value==""). This means "If the box named userName of the form named login contains nothing, then...". return false. This is used to stop the form from submitting. By default, a form will return true if submitting. <span class="code-keyword">return</span> validate() That means, "if submitting, then call the function validate()<span class="code-string">"</span><span class="code-string"></span>.

Protect a file by using Login

Let's try an example

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>SCRIPT</SPAN> <SPAN class=code-attribute>Language</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>JavaScript"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword">function</span> checkLogin(x) 
{ 
<span class="code-keyword">if</span> ((x.id.value != <span class="code-string">"</span><span class="code-string">Sam"</span>)||(x.pass.value !=<span class="code-string">"</span><span class="code-string">Sam123"</span>)) 
{ 
  alert(<span class="code-string">"</span><span class="code-string">Invalid Login"</span>); 
  <span class="code-keyword">return</span> <span class="code-keyword">false</span>; 
} 
<span class="code-keyword">else</span> 
  location=<span class="code-string">"</span><span class="code-string">main.htm"</span> 
} 
<span class="code-keyword"></</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span>
Copy after login
<span class="code-keyword"><</SPAN>/head<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>body<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>p<SPAN class=code-keyword>></span>UserID:<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>text"</SPAN> name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>id"</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/p<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>p<SPAN class=code-keyword>></span>Password:<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>password"</SPAN> name=<SPAN class=code-string>"</SPAN><SPAN class=code-string>pass"</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/p<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>p<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Login"</SPAN> onClick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>checkLogin(this.form)"</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/p<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/body<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/html<SPAN class=code-keyword>></span>
Copy after login

|| means "or", and ,!= indicates "not equal". So we can explain the script: "If the id does not equal 'Sam', or the password does not equal 'Sam123', then show an alert ('Invalid Login') and stop submitting. Else, open the page 'main.htm'".


Back to top

Link

In most cases, a form can be repaced by a link:

<span class="code-keyword"><</SPAN>a href=<SPAN class=code-string>"</SPAN><SPAN class=code-string>JavaScript:window.location.reload()"</SPAN><SPAN class=code-keyword>></span>Click to reload!<span class="code-keyword"><</SPAN>/a<SPAN class=code-keyword>></span>
Copy after login

More examples:

<span class="code-keyword"><</SPAN>a href=<SPAN class=code-string>"</SPAN><SPAN class=code-string>#"</SPAN> onClick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>alert('Hello, world!')"</SPAN><SPAN class=code-keyword>></span>Click me to say Hello<span class="code-keyword"><</SPAN>/a<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>br<SPAN class=code-keyword>></span>
Copy after login
<span class="code-keyword"><</SPAN>a href=<SPAN class=code-string>"</SPAN><SPAN class=code-string>#"</SPAN> onMouseOver=<SPAN class=code-string>"</SPAN><SPAN class=code-string>location='main.htm'"</SPAN><SPAN class=code-keyword>></span>Mouse over to see Main Page<span class="code-keyword"><</SPAN>/a<SPAN class=code-keyword>></span>
Copy after login

Back to top

Date

Let's see an example:

<span class="code-keyword"><</SPAN>HTML<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>HEAD<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>TITLE<SPAN class=code-keyword>></span>Show 
Date<span class="code-keyword"><</SPAN>/TITLE<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/HEAD<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>BODY<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>SCRIPT LANGUAGE=<SPAN class=code-string>"</SPAN><SPAN class=code-string>JavaScript"</SPAN><SPAN class=code-keyword>></span> 
var x= <span class="code-keyword">new</span> Date(); 
document.write (x); 
<span class="code-keyword"><</SPAN>/SCRIPT<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/BODY<SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN>/HTML<SPAN class=code-keyword>></span>
Copy after login

To activate a Date Object, you can do this: var x=new Date(). Whenever you want to create an instance of the date object, use this important word: new followed by the object name().

Dynamically display different pages

You can display different pages according to the different time. Here is an example:

var banTime= <span class="code-keyword">new</span> Date() 
var ss=banTime.getHours() 
<span class="code-keyword">if</span> (ss<span class="code-keyword"><</SPAN>=12) 
  document.write(<SPAN class=code-string>"</SPAN><SPAN class=code-string><img src='banner1.gif'>"</span>) 
<span class="code-keyword">else</span> 
  document.write(<span class="code-string">"</span><span class="code-string"><img src='banner2.gif'>"</span>) 
Copy after login
Date object
Methods
getDate
getTime
getTimezoneOffset
getDay
getMonth
getYear
getSeconds
getMinutes
getHours

 

Window

Open a window

To open a window, simply use the method "window.open()":

<span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Click here to see"</SPAN> onclick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>window.open('test.htm')"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span> 
Copy after login

You can replace test.htm with any URL, for example, with http:<span class="code-comment">//</span><span class="code-comment">www.yahoo.com</span>.

Size, toolbar, menubar, scrollbars, location, status

Let's add some of attributes to the above script to control the size of the window, and show: toolbar, scrollbars etc. The syntax to add attributes is:

open(<span class="code-string">"</span><span class="code-string">URL"</span>,<span class="code-string">"</span><span class="code-string">name"</span>,<span class="code-string">"</span><span class="code-string">attributes"</span>)
Copy after login

For example:

<span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Click here to see"</SPAN> 
onclick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>window.open('page2.htm','win1','width=200,height=200,menubar')"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span> 
Copy after login

Another example with no attributes turned on, except the size changed:

<span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Click here to see"</SPAN> 
onclick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>window.open('page2.htm','win1','width=200,height=200')"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span>
Copy after login

Here is the complete list of attributes you can add:

width height toolbar
location directories status
scrollbars resizable menubar

Reload

To reload a window, use this method:

window.location.reload()
Copy after login

Close Window

Your can use one of the codes shown below:

<span class="code-keyword"><</SPAN>form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>input type=<SPAN class=code-string>"</SPAN><SPAN class=code-string>button"</SPAN> value=<SPAN class=code-string>"</SPAN><SPAN class=code-string>Close Window"</SPAN> onClick=<SPAN class=code-string>"</SPAN><SPAN class=code-string>window.close()"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>/form<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>a href=<SPAN class=code-string>"</SPAN><SPAN class=code-string>javascript:window.close()"</SPAN><SPAN class=code-keyword>></span>Close Window<span class="code-keyword"><</SPAN>/a<SPAN class=code-keyword>></span>
Copy after login

Loading

The basic syntax when loading new content into a window is:

window.location=<span class="code-string">"</span><span class="code-string">test.htm"</span>
Copy after login

This is the same as

<span class="code-keyword"><</SPAN>a href=<SPAN class=code-string>"</SPAN><SPAN class=code-string>test.htm>Try this </a></span>
Copy after login

Let's provide an example, where a confirm box will allow users to choose between going to two places:

<span class="code-keyword"><</SPAN>script<SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN>!-- 
function ss() 
{ 
var ok=confirm(<SPAN class=code-string>'</SPAN><SPAN class=code-string>Click "OK" to go to yahoo, "CANCEL" to go to hotmail'</SPAN>) 
<SPAN class=code-keyword>if</SPAN> (ok) 
location=<SPAN class=code-string>"</SPAN><SPAN class=code-string>http://www.yahoo.com"</SPAN> 
<SPAN class=code-keyword>else</SPAN> 
location=<SPAN class=code-string>"</SPAN><SPAN class=code-string>http://www.hotmail.com"</SPAN> 
} 
<SPAN class=code-comment>//</SPAN><SPAN class=code-comment>--> 
</span> 
<span class="code-keyword"><</SPAN>/script<SPAN class=code-keyword>></span>
Copy after login

Remote Control Window

Let's say you have opened a new window from the current window. After that, you will wonder how to make a control between the two windows. To do this, we need to first give a name to the window.Look at below:

aa=window.open(<span class="code-string">'</span><span class="code-string">test.htm'</span>,<span class="code-string">'</span><span class="code-string">'</span>,<span class="code-string">'</span><span class="code-string">width=200,height=200'</span>)
Copy after login

By giving this window a name "aa", it will give you access to anything that's inside this window from other windows. Whenever we want to access anything that's inside this newly opened window, for example, to write to this window, we would do this: aa.document.write("This is a test.").

Now, let's see an example of how to change the background color of another window:

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>form</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>input</SPAN> <SPAN class=code-attribute>type</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>button"</SPAN> <SPAN class=code-attribute>value</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>Open another page"</SPAN> 
<SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>aa=window.open('test.htm','','width=200,height=200')"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>input</SPAN> <SPAN class=code-attribute>type</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>radio"</SPAN> <SPAN class=code-attribute>name</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>x"</SPAN> <SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>aa.document.bgColor='red'"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>input</SPAN> <SPAN class=code-attribute>type</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>radio"</SPAN> <SPAN class=code-attribute>name</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>x"</SPAN> <SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>aa.document.bgColor='green'"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>input</SPAN> <SPAN class=code-attribute>type</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>radio"</SPAN> <SPAN class=code-attribute>name</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>x"</SPAN> <SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>aa.document.bgColor='yellow'"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>form</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

opener

Using <span class="code-string">"</span><span class="code-string">opener"</span> property, we can access the main window from the newly opened window.

Let's create Main page:

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>form</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>input</SPAN> <SPAN class=code-attribute>type</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>button"</SPAN> <SPAN class=code-attribute>value</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>Open another page"</SPAN> 
<SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>aa=window.open('test.htm','','width=100,height=200')"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>form</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

Then create Remote control page (in this example, that is test.htm):

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>title</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword">function</span> remote(url){ 
window.opener.location=url 
} 
<span class="code-keyword"></</SPAN><SPAN class=code-leadattribute>script</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>head</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>p</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>a</SPAN> <SPAN class=code-attribute>href</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>#"</SPAN> <SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>remote('file1.htm')"</SPAN><SPAN class=code-keyword>></span>File 
1<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>a</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>p</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>p</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>a</SPAN> <SPAN class=code-attribute>href</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>#"</SPAN> <SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>remote('file2.htm')"</SPAN><SPAN class=code-keyword>></span>File 
2<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>a</SPAN><SPAN class=code-keyword>></span><span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>p</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

Try it now!


Frame

One of the most popular uses of loading multiple frames is to load and change the content of more than one frame at once. Lets say we have a parent frame:

<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>frameset</SPAN> <SPAN class=code-attribute>cols</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>150,*"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>frame</SPAN> <SPAN class=code-attribute>src</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>page1.htm"</SPAN> <SPAN class=code-attribute>name</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>frame1"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>frame</SPAN> <SPAN class=code-attribute>src</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>page2.htm"</SPAN> <SPAN class=code-attribute>name</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>frame2"</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>frameset</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

We can add a link in the child frame "frame1" that will change the contents of not only page1, but page2 too. Shown below is the html code for it:

 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>h2</SPAN><SPAN class=code-keyword>></span>This is page 1 <span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>h2</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-leadattribute>a</SPAN> <SPAN class=code-attribute>href</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>page3.htm"</SPAN> 
<SPAN class=code-attribute>onClick</SPAN><SPAN class=code-keyword>="</SPAN><SPAN class=code-keyword>parent.frame2.location='page4.htm'"</SPAN><SPAN class=code-keyword>></span>Click Here<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>a</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>body</SPAN><SPAN class=code-keyword>></span> 
<span class="code-keyword"><</SPAN><SPAN class=code-keyword>/</SPAN><SPAN class=code-leadattribute>html</SPAN><SPAN class=code-keyword>></span>
Copy after login

Notice: You should use <span class="code-string">"</span><span class="code-string">parent.frameName.location"</span> to access another frame. "parent" standards for the parent frame containing the frameset code.


 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template