Use a link to JavaScript notes to redirect users

Source: Internet
Author: User

You can seamlessly redirect users to another page based on whether they have enabled the JavaScript function. This example shows how to embed the redirection function into the link. Two HTML pages and a JavaScript file will be used below.
The first HTML page shows the connection to the user:
Test.html
[Html]
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Welcome to our site </title>
<Script type = "text/javascript" src = "script01.js">
</Script>
</Head>
<Body bgcolor = "# FFFFFF">
<H2 align = "center">
<A href = "script01.html" id = "redirect"> Welcome to our site... c 'mon in! </A>
</H2>
</Body>
</Html>
The second HTML page is the HTML page that the user is redirected to when the JavaScript function is enabled.
Jswelcome.html
[Html]
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Our site </title>
</Head>
<Body bgcolor = "# FFFFFF">
<H1> Welcome to our web site, which features lots of cutting-edge JavaScript </Body>
</Html>
Script01.js
[Javascript]
Window. onload = initAll;
 
Function initAll (){
Document. getElementById ("redirect"). onclick = initRedirect;
}
 
Function initRedirect (){
Window. location = "jswelcome.html ";
Return false;
}
 
Script01.html
[Html]
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> My JavaScript page </title>
</Head>
<Body bgcolor = "# FFFFFF">
<Noscript>
<H2> This page requires JavaScript. </Noscript>
</Body>
</Html>
When the user opens "test.html", their users enable the javascriptfunction and will be taken to one of the script01.htmlor jswelcome.html pages.
In the js file
[Javascript]
Function initRedirect (){
Window. location = "jswelcome.html ";
Return false;
}
If you call this function, it sets window. location (the page displayed in the browser) to a new page. Return false indicates that the processing of user clicks is stopped, so that the page specified in the href page is not loaded. the coolest feature of this method is that, after the redirection is completed, the user will not realize that the page has been redirected. (Source JavaScript basic tutorial)

From Yanghai

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.