Pasre URL Vars

Benchmark created on


Preparation HTML

<h2>URL mus have a ?id=[some value]</h2>

<p>ID = <span id="value"></span></p>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
Semantic Code
//Function to get any var from the URL

function getURLVar(varname) {
 var varLength = varname.length + 1; //Get the length of the variable used, plus 1 for the equals sign
 var URLVar;
 if (location.href.search(varname + "=") > 0) {
  var varNum = location.href.search(varname + "=") + varLength; //Find position where var value starts
  URLVar = location.href.substring(varNum, location.href.length); //Get substring from start of value to end of URL
  if (URLVar.search(/&/) > 0) {
   URLVar = URLVar.substring(0, URLVar.search(/&/)); //If there are multiple variables, stop at the first '&' sign
  }
 }
 return URLVar;
}

$('#value').text(getURLVar('id'));
ready
RegEx Version
function getURLVar(key, default_) {
 if (default_ === null) {
  default_ = "";
 }
 key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
 var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
 var qs = regex.exec(window.location.href);
 if (qs === null) {
  return default_;
 }
 else {
  return qs[1];
 }
}

$('#value').text(getURLVar('id'));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.