Javascript Course

The function presented in this page ( urlData() ) can be used to get /extract the domain name and other data from URL address.
The function receives a string with the URL address, and returns an object containing these data:
Protocol, Page Path, File-name, search parts, port, and hash part.

Code of the function (click to select it):
// Extract data from URL address
// Returns an object with parts of the URL address:
// protocol, domain-name, port, path, file-name, search-string, search-object, hash
function urlData(url){ // From: https://coursesweb.net/javascript/
  // object that will be returned
  var re = {protocol:'', domain:'', port:80, path:'', file:'', search_str:'', search_obj:{}, hash:''};

  // creates an anchor element, and adds the url in "href" attribute
  var a_elm  = document.createElement('a');
  a_elm.href = url;

  // adds URL data in re object, and returns it
  re.protocol = a_elm.protocol.replace(':', '');
  re.domain = a_elm.hostname.replace('www.', '');
  if(a_elm.port !='') re.port = a_elm.port;
  re.path = a_elm.pathname;
  if(a_elm.pathname.match(/[^\/]+[\.][a-z0-9]+$/i) != null) re.file = a_elm.pathname.match(/[^\/]+[\.][a-z0-9]+$/i)[0];
  re.search_str = a_elm.search.replace('?', '');

  //get search-data into an object {name:value}, in case there are multiple pairs name=value
  var src_data = re.search_str.split('&');
  for(var i=0; i<src_data.length; i++){
    var ar_val = src_data[i].split('=');   //separate name and value from each pair
    re.search_obj[ar_val[0]] = ar_val[1];
  }

  re.hash = a_elm.hash.replace('#', '');  //get #hash part
  return re;
}
Example:
<div id="urld">Here it is added URL data.</div>
<script>
// Extract data from URL address
// Returns an object with parts of the URL address:
// protocol, domain-name, port, path, file-name, search-string, search-object, hash
function urlData(url){ // From: https://coursesweb.net/javascript/
  // object that will be returned
  var re = {protocol:'', domain:'', port:80, path:'', file:'', search_str:'', search_obj:{}, hash:''};

  // creates an anchor element, and adds the url in "href" attribute
  var a_elm  = document.createElement('a');
  a_elm.href = url;

  // adds URL data in re object, and returns it
  re.protocol = a_elm.protocol.replace(':', '');
  re.domain = a_elm.hostname.replace('www.', '');
  if(a_elm.port !='') re.port = a_elm.port;
  re.path = a_elm.pathname;
  if(a_elm.pathname.match(/[^\/]+[\.][a-z0-9]+$/i) != null) re.file = a_elm.pathname.match(/[^\/]+[\.][a-z0-9]+$/i)[0];
  re.search_str = a_elm.search.replace('?', '');

  //get search-data into an object {name:value}, in case there are multiple pairs name=value
  var src_data = re.search_str.split('&');
  for(var i=0; i<src_data.length; i++){
    var ar_val = src_data[i].split('=');   //separate name and value from each pair
    re.search_obj[ar_val[0]] = ar_val[1];
  }

  re.hash = a_elm.hash.replace('#', '');  //get #hash part
  return re;
}

// Test
var url = 'https://coursesweb.net:80/javascript/index.php?pg=tutorial&id=89#hash_part';
var url_data = urlData(url);

// displays URL data in #urld
document.getElementById('urld').innerHTML = 'protocol : '+ url_data.protocol +'<br>domain : '+ url_data.domain +'<br>port : '+ url_data.port +'<br>path : '+ url_data.path +'<br>file : '+ url_data.file +'<br>search_str : '+ url_data.search_str +'<br>search_obj : '+ JSON.stringify(url_data.search_obj) +'<br>hash : '+ url_data.hash;
</script>
Result:
protocol : http
domain : coursesweb.net
port : 80
path : /javascript/index.php
file : index.php
search_str : pg=tutorial&id=89
search_obj : {"pg":"tutorial", "id":"89"}
hash : hash_part

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Get Domain Name and other Data from URL

Last accessed pages

  1. Add, Change, and Remove Attributes with jQuery (46356)
  2. Node.js Move and Copy file (28419)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141747)
  4. Rectangle, Oval, Polygon - Star (3322)
  5. PHP PDO - prepare and execute (9187)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (471)
  2. CSS cursor property - Custom Cursors (78)
  3. The Mastery of Love (69)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)