- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
Javascript麻瓜的練習:Parse URL
Exercise for Javascript Muggle: Parse URL
Create a function called parse_URL to parse the URL: https://example.com:4000/folder/page.html?x=y&a=b#section-2
//please refer the link to complete the exercise: https://developer.mozilla.org/zh-TW/docs/Web/API/URL/URL
The function should print the following text to the console:
- source: https://example.com:4000/folder/page.html?x=y&a=b#section-2
- protocol: https
- host: example.com
- port: 4000
- query:x=y,a=b
- path: /folder/page.html
- hash: section-2
- segments: [ '', 'folder', 'page.html' ]
- Sample codes
- =============================================================================
- function parse_URL(myURL) {
- var a = new URL(myURL);
- urlObj= {
- source: myURL,
- host: a.hostname,
- query: a.search.replace('?','').split('&'),
- hash: a.hash.replace('#',''),
- };
- return urlObj;
- }
- parse_URL('https://example.com:4000/folder/page.html?x=y&a=b#section-2');
- console.log('source: '+urlObj.source);
- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
留言