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: 

  • 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);

留言

這個網誌中的熱門文章