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.
<script type="text/javascript"><!--
// 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 = 'https://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
alert('name: '+ name + '\n 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 definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Get search data from URL address in JavaScript

Last accessed pages

  1. SHA256 Encrypt hash in JavaScript (5787)
  2. Register and show online users and visitors (22779)
  3. innerHTML and outerHTML to Get and Replace HTML content (17554)
  4. Node.js Move and Copy file (3517)
  5. Add, Change, and Remove Attributes with jQuery (36000)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (447)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (352)
  3. Register and show online users and visitors (321)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (302)
  5. Insert, Select and Update NULL value in MySQL (214)