Javascript Course

In this page it is presented a function that can be used to get search data from URL address in JavaScript, data added after the '?' character.
The function presented bellow receive the URL address, then returns an object with elements "name: value" formed with the "name=value" pairs from that URL.

Function code:
// Returns an object with elements 'name: value' with data ftom URL (the 'name=value' pairs)
function getDataUrl(url) {
 // https://coursesweb.net/javascript/
 var url_data = url.match(/\?([^#]*)/i)[1]; // gets the string between '?' and '#'

 // separate the data into an array, in case the are multiple pairs name=value
 var ar_url_data = url_data.split('&');

 // traverse the array, and adds into an object elements name:value
 var data_url = {};
 for(var i=0; i<ar_url_data.length; i++) {
 var ar_val = ar_url_data[i].split('='); // separate name and value from each pair
 data_url[ar_val[0]] = ar_val[1];
 }

 return data_url;
}

Example of usage getDataUrl() function

<h4>Example getting data from URL query string</h4>
<p>Gets and shows the value of 'name' and 'id' from this URL:<br>
 <b>//marplo.net/?name=Mar&amp;id=10</b></p>

<script>
// Returns an object with elements 'name: value' with data ftom URL (the 'name=value' pairs)
function getDataUrl(url) {
 // https://coursesweb.net/javascript
 var url_data = url.match(/\?([^#]*)/i)[1]; // gets the string between '?' and '#'

 // separate the data into an array, in case the are multiple pairs name=value
 var ar_url_data = url_data.split('&');

 // traverse the array, and adds into an object elements name:value
 var data_url = {};
 for(var i=0; i<ar_url_data.length; i++) {
 var ar_val = ar_url_data[i].split('='); // separate name and value from each pair
 data_url[ar_val[0]] = ar_val[1];
 }

 return data_url;
}

// Using getDataUrl()

var url = '//marplo.net/?name=Mar&id=10';
var data_url = getDataUrl(url); // gets the object with 'name:value' data
var name = data_url.name;
var id = data_url.id;

// test
document.write('name: '+ name +'<br> id: '+ id);
</script>
Demo (click on the button):

• You can get the URL address of current page using: window.location . So, if you want to use in your script the URL address of current page, replace in the code above with this code:
var url = window.location;

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 search data from URL address in JavaScript

Last accessed pages

  1. Get Mime Type of file or string content in PHP (6228)
  2. Get and Modify content of an Iframe (32346)
  3. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (59589)
  4. querySelector and querySelectorAll (30124)
  5. sPBM - Simple PHP Backup Manager (3402)

Popular pages this month

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