JavaScript get Path Design source code

Source: Internet
Author: User

1. Design source code
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>

<Title> JavaScript retrieval path </title>

<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<Script type = "text/javascript">
Function findPath ()
{
// Obtain the current URL
Var curNetAddr = too many Doc ument. location. href;
Alert ("Get current URL:" + curNetAddr );

// Obtain the directory after the host address
Var hostPath = specified parameter Doc ument. location. pathname;
Alert ("directory after obtaining the host address:" + hostPath );

// Returns the position of the first occurrence of a specified string value.
Var count = curNetAddr. indexOf (hostPath );
Alert ("return the position where a specified string value first appears in the string:" + count );

// Obtain the host address
Var hostAddr = curNetAddr. substring (0, count );
Alert ("Get host address:" + hostAddr );

// Obtain the project name "/"
Var projectName = hostPath. substring (0, hostPath. substr (1). indexOf ('/') + 1 );
Alert ("Get project name with"/":" + projectName );

// Obtain the project path
Var path = hostAddr + projectName;
Alert ("Get project path:" + path );
}
</Script>

</Head>

<Body>
<Div id = "body_div">
<Input type = "button" id = "btn" value = "obtain path" onclick = "findPath ()"/>
</Div>
</Body>
</Html>

2. design results

(1) During Initialization

(2) obtain the current website

 

(3) directory after obtaining the host address

 

(4) return the position where a specified string value first appears in the string.

(5) obtain the host address

(6) obtain the project name "/"

(7) obtain the project path

Related Article

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.