url.md

2018-11-22 18:05 更新

ngui/url

executable()

獲取當(dāng)前應(yīng)用程序的二進(jìn)制執(zhí)行文件路徑

Example:

// Prints:
// file:///var/containers/Bundle/Application/4F1BD659-601D-4932-8484-D0D1F978F0BE/test.app/test
console.log(url.executable());

documents([appendPath])

獲取當(dāng)前應(yīng)用程序的文檔存儲(chǔ)路徑

  • @arg [appendPath=''] {String} 追加到文檔路徑后面
  • @ret {String}

Example:

// Prints:
// file:///var/mobile/Containers/Data/Application/89A576FE-7BB9-4F26-A456-E9D7F8AD053D/Documents
console.log(url.documents());
// Prints 設(shè)置追加路徑參數(shù)的結(jié)果:
// file:///var/mobile/Containers/Data/Application/89A576FE-7BB9-4F26-A456-E9D7F8AD053D/Documents/aa.jpeg
console.log(url.documents('aa.jpeg'));

temp([appendPath])

獲取應(yīng)用程序臨時(shí)目錄

resources([appendPath])

獲取應(yīng)用程序資源目錄

fallback(path)

恢復(fù)路徑為操作系統(tǒng)可以識(shí)別的路徑,一般不需要使用該函數(shù),除非直接調(diào)用非NGUI提供的Native/C/C++函數(shù)

Example:

// Prints: /var/data/test.js
console.log(url.fallback('file:///var/data/test.js'));

cwd()

獲取當(dāng)前工作目錄

chdir(path)

設(shè)置當(dāng)前工作目錄,成功后返回true

isAbsolute(path)

測(cè)試路徑是否為一個(gè)絕對(duì)路徑

Example:

// Prints:
// true
// true
// false
console.log(url.isAbsolute('/var/kk'));
console.log(url.isAbsolute('http://nodegui.org/'));
console.log(url.isAbsolute('index.jsx'));

resolve(path,[...partPaths])

格式化傳入的路徑為標(biāo)準(zhǔn)絕對(duì)路徑

  • @arg path {String} 傳入路徑
  • @arg [...partPaths] {String} 可選的分部路徑
  • @ret {String}

Example:

// Prints: http://nodegui.org/A/C/test.js
console.log(url.resolve('http://nodegui.org/home', "..", "A", "B", "..", "C", "test.js"));
// Prints: 
// true
// file:///var/data/aaa/cc/ddd/kk.jpg
console.log(url.chdir('/var/data'));
console.log(url.resolve('aaa/bbb/../cc/.///ddd/kk.jpg'));

Class: URL

url與path處理類(lèi)

URL.constructor([path])

構(gòu)造函數(shù),如果傳入非法路徑會(huì)拋出異常

  • @arg [path=''] {String} 字符串路徑,傳入相對(duì)路徑或決對(duì)路徑

Example:

// cwd: file:///var/data
// Prints: file:///var/data/index.js
var uri = new URL('index.js');
console.log(uri.href);
// Prints: http://nodegui.org/index.html?args=0
var uri2 = new URL('http://nodegui.org/home/../index.html?args=0')
console.log(uri2.href);
// Prints: 
// Error: Parse uri error, Illegal URL
new URL('http://nodegui.org:').href

Get: URL.href

獲取uri完整的href,包括參數(shù)

Example:

// Prints: http://nodegui.org/
console.log(new URL('http://nodegui.org/').href);

Get: URL.filename

獲取文件名稱(chēng)

// Prints: /aaa/bbbb/ccc/test.js
console.log(new URL('http://nodegui.org/aaa/bbbb/ccc/test.js').filename);

Get: URL.dirname

獲取目錄名稱(chēng)

Example:

// Prints: /aaa/bbbb/ccc
console.log(new URL('http://nodegui.org/aaa/bbbb/ccc/test.js').dirname);

Get: URL.search

獲取uri查詢(xún)參數(shù)

Example:

// Prints: ?a=A&b=B
console.log(new URL('http://nodegui.org/?a=A&b=B').search);

Get: URL.hash

獲取hash參數(shù)

Example:

// Prints: #c=C&d=D
console.log(new URL('http://nodegui.org/?a=A&b=B#c=C&d=D').hash);

Get: URL.host

獲取主機(jī),返回一個(gè)帶端口號(hào)的主機(jī)名稱(chēng)

Example:

// Prints: nodegui.org:80
console.log(new URL('http://nodegui.org:81/').host);

Get: URL.hostname

獲取主機(jī)名稱(chēng),不會(huì)返回端口號(hào)

Example:

// Prints: nodegui.org
console.log(new URL('http://nodegui.org:81/').host);

Get: URL.origin

獲取uri起源,protocol+host

Example:

// Prints: http://nodegui.org:81
console.log(new URL('http://nodegui.org:81/host/index.html').host);
// Prints: file://
console.log(new URL('file:///var/data/index.html').host);

Get: URL.basename

獲取基礎(chǔ)文件名稱(chēng)

Example:

// Prints: index.html
console.log(new URL('file:///var/data/index.html').basename);

Get: URL.extname

獲取文件擴(kuò)展名稱(chēng)

Example:

// Prints: .html
console.log(new URL('file:///var/data/index.html').extname);

Get: URL.port

獲取主機(jī)端口號(hào),如果URL中沒(méi)有定義端口號(hào)返回一個(gè)空字符串

Example:

// Prints: 81
console.log(new URL('http://nodegui.org:81').port);
// Prints 沒(méi)有端口號(hào)會(huì)返回空字符串: ""
console.log(new URL('http://nodegui.org').port);

Get: URL.protocol

獲取URL的協(xié)議類(lèi)型字符串, 例如: 'http:'|'https'|'ftp:'

Get: URL.params

以對(duì)像方式返回查詢(xún)參數(shù)集合

Example:

// Prints:
// {
//   a: "100",
//   b: "test"
// }
console.log(new URL('http://nodegui.org/?a=100&b=test').params);

Get: URL.hashParams

以對(duì)像方式返回Hash參數(shù)集合

Example:

// Prints:
// {
//   a: "200",
//   b: "300"
// }
console.log(new URL('http://nodegui.org/#a=200&b=300').hashParams);

URL.getParam(name)

通過(guò)名稱(chēng)獲取uri參數(shù)值

Example:

// Prints: ok
console.log(new URL('http://nodegui.org/?args=ok').getParam('args'));

URL.setParam(name, value)

設(shè)置URL查詢(xún)參數(shù)鍵值對(duì),并返回自己

  • @arg name {String}
  • @arg value {String}
  • @ret {URL} 返回自己

URL.deleteParam(name)

通過(guò)名稱(chēng)刪除URL查詢(xún)參數(shù)

  • @arg name {String}
  • @ret {URL}

URL.clearParam()

刪除URL中的所有查詢(xún)參數(shù)

  • @ret {URL}

URL.getHash(name)

URL.setHash(name, value)

URL.deleteHash(name)

URL.clearHash()

URL.relative(target)

返回與target的相對(duì)路徑

  • @arg target {String} 字符串類(lèi)型的目標(biāo)路徑
  • @ret {String}

Example:

// Prints: ../A/B/C/test.js
var uri = new URL('http://nodegui.org/home/');
console.log(uri.relative('http://nodegui.org/A/B/C/test.js'));
// Prints: file:///var/data/A/B/C/test.js
var uri2 = new URL('http://nodegui.org/home/');
console.log(uri2.relative('file:///var/data/A/B/C/test.js'));

下面為URL快捷函數(shù),其中第一個(gè)參數(shù)都為創(chuàng)建URL對(duì)像用到的路徑

filename(path)

dirname(path)

search(path)

hash(path)

host(path)

hostname(path)

origin(path)

basename(path)

extname(path)

port(path)

protocol(path)

params(path)

hashParams(path)

getParam(path, name)

setParam(path, name, value)

delParam(path, name)

clearParam(path)

getHash(path, name)

setHash(path, name, value)

deleteHash(path, name)

clearHash(path)

relative(path, target)

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)