Home Web Front-end JS Tutorial javascript parseUrl function (getting URL parameters from abroad)_javascript skills

javascript parseUrl function (getting URL parameters from abroad)_javascript skills

May 16, 2016 pm 06:25 PM
url parameters URL

函数代码

复制代码 代码如下:

function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;iif (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};
}

用法:
复制代码 代码如下:

var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
myURL.file; // = 'index.html'
myURL.hash; // = 'top'
myURL.host; // = 'abc.com'
myURL.query; // = '?id=255&m=hello'
myURL.params; // = Object = { id: 255, m: hello }
myURL.path; // = '/dir/index.html'
myURL.segments; // = Array = ['dir', 'index.html']
myURL.port; // = '8080'
myURL.protocol; // = 'http'
myURL.source; // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'

演示代码:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enter the URL in steam? How to open a website on steam How to enter the URL in steam? How to open a website on steam Mar 14, 2024 pm 12:10 PM

If you want to check some relevant information in Steam or share some website links with friends, how do you do it? Can I enter a URL into steam? Of course it is possible. Below I will teach you how to open a URL in Steam. Specific methods: 1. First, we open steam. 2. Click the Settings option. 3. Then switch to the interface tab. 4. Check Show address bar. 5. Then enter the URL in the address bar to use the browser.

What does php detect URL mean? What does php detect URL mean? Jul 18, 2023 am 11:02 AM

PHP URL detection refers to using the PHP programming language to verify whether the input string conforms to the format of the URL. Methods to detect URLs: 1. Use regular expressions for URL verification. You can use the "preg_match" function to perform regular matching, if the URL matches the pattern; 2. Use built-in functions for URL verification, use the "filter_var" function and "FILTER_VALIDATE_URL" ” filter to verify.

Teach you how to use PHP to remove the suffix of the URL path Teach you how to use PHP to remove the suffix of the URL path Mar 21, 2024 pm 03:39 PM

Teach you how to use PHP to remove the suffix of the URL path. In website development, you often encounter the need to remove the suffix of the URL path to achieve a more beautiful and standardized URL. Today we will learn how to use PHP to remove the suffix of the URL path, let us explore this issue together. First, we need to clarify what effect we want to achieve. Usually, the URL path suffix refers to the file extension in the URL, such as .php, .html, etc. Our goal is to automatically go to the URL when the user accesses the URL with the suffix

How to match URL parameters using regular expressions in Go language How to match URL parameters using regular expressions in Go language Jul 12, 2023 pm 03:46 PM

How to use regular expressions to match URL parameters in Go Regular expressions are a powerful tool that can be used to match and process strings. In web development, it is often necessary to extract specific parameters from the URL. Go language provides the regular expression package (regexp) in the standard library, which can easily use regular expressions to match URL parameters. This article will introduce how to use regular expressions to match URL parameters in the Go language and provide some practical code examples. Import the regexp package First, we need to import

What is the URL for downloading the win10 image? What is the URL for downloading the win10 image? Jul 10, 2023 am 09:25 AM

Where can I download genuine Microsoft win10 image system iso? Nowadays, win10 system software has gradually become a popular operating system, and many users will choose this system software. So next, the editor will tell you about the website where you can download the win10 image system. What is there? What is the win10 image download website? 1. Go to Microsoft Windows 10 official download tool page, enter the query to download the tool and click to download. Website: https://www.microsoft.com/zhcn/softwaredownload/windows10. 2. Start downloading the excellent Windows 10 download tool. If you only want to download the system image, then

PHP regular expression in action: matching URL parameters PHP regular expression in action: matching URL parameters Jun 23, 2023 am 10:55 AM

With the development of Internet technology and the continuous expansion of applications, URL parameters have become a common data type that needs to be processed in our daily development. In the actual development environment, we often need to match some specific information based on URL parameters, such as extracting the value of a certain parameter, determining whether the parameter conforms to the format, etc. Regular expressions can help us quickly match and process URL parameters. This article will introduce the relevant knowledge and techniques used for URL parameter matching in PHP regular expression practice, and give example code to implement it.

How to set the URL for tplink router How to set the URL for tplink router Mar 06, 2024 pm 04:10 PM

Setting method: 1. First open the browser and enter "192.168.0.1, 192.168.1.1"; 2. Enter the password and username account behind the router; 3. After logging in to the setting interface, you can follow the wizard steps; 4. , then you can set the Internet access method, there are three methods available; 5. Select pppoe Internet access and you need to fill in the account password to operate; 6. Perform wireless settings according to needs; 7. Wait for success.

ok trading platform website 2023 ok trading platform official website update link ok trading platform website 2023 ok trading platform official website update link Feb 07, 2024 am 11:10 AM

New users download and win blind boxes up to 60,000 yuan. The official website of OK Exchange is a website with one of the largest accounts among virtual currency exchanges. Its rich trading modes and multiple digital asset types allow you to enjoy different trading experiences. If you need it, you can click on the OK exchange URL at the top of the page to enter and download. AndroidOKX6.4.0 has been released to the official website (update time: 2023.3.4) Trading: – Commission aggregation of all order types – K-line buying and selling points 2nd period – Contract DCA – Copying Square online search function Web3: – Import wallet list to increase address display – gasPrice multiple adjustment – ​​Gas gas station supports Tron chain – Farm increases Pool income Fin

See all articles