Query String Example

This page uses a custom JavaScript object (query_string.js) that I created for reading/writing query strings. View source of this page and/or see code examples below.

The table below will display any arguments you add to the query string in this browser address bar. Or, if there is no query string, it will display a message saying so.

Try It: Follow These Instructions Then Check Out Results Section

  1. To the URL in this Browser window, add a query string like ?first_name=Danny&last_name=Devito. It would look like this in your Web browser address field: http://dhtmldev.com/examples/js/query_string/index.htm?first_name=Danny&last_name=Devito.
  2. Hit the enter key (do not refresh the page, which would just reload the old URL).

Results Section

Code

<script type="text/javascript">

// Create a new QueryString object
var myQuery = new QueryString();
// Read query string from browser into the new QueryString object, name myQuery
myQuery.read();
// Check the status, to make sure it read the query string
// Then write out the query string arguments
if(myQuery.getStatus())
{	var sHtml = "<table border='1'>";
	sHtml += "<caption>Query String Data</caption>";
	sHtml += "<thead><tr><th>Name</th><th>Value</th></tr></thead>";
	var aQueryData = myQuery.getAll();
	for(var n in aQueryData)
	{	sHtml += "<tr><td>"+n+"</td>" + "<td>"+aQueryData[n]+"</td></tr>";
	}
	sHtml += "</table>";
	document.write(sHtml);
}
else
{	document.write("<p>No query string found.</p>");
}

</script>