Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to get user name using url parameters(wwww.domain.com/upload.html?user=user1), here my code, i want to show user name in html form input text box... please help me........

HTML
<!DOCTYPE html>
<html>
<head>
<script>
var QueryString = function () {
  // This function is anonymous, is executed immediately and 
  // the return value is assigned to QueryString!
  var query_string = {};
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    	// If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = pair[1];
    	// If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]], pair[1] ];
      query_string[pair[0]] = arr;
    	// If third or later entry with this name
    } else {
      query_string[pair[0]].push(pair[1]);
    }
  } 
    return query_string;
} 
</script>
</head>
<body>

<center>
<h2>Java Uploader Beta <BR> </h2></center>
<form id="upload_form" enctype="multipart/form-data" method="post">
<label for="user">User:</label>
<input name="user" type="text" id="user"  tabindex="1" value="QueryString.user();" readonly/><br><BR>
<label for="title">Title:</label>
<input type="text" id="title" name="title"  tabindex="1"/><br><BR>
<label for="artist">Artist:</label>
<input type="text" id="artist" name="artist"  tabindex="2"/><br><BR>
<label for="year">Year:</label>
<input type="text" id="year" name="year"  tabindex="3"/><br><BR>
<label for="album">Album:</label>
<input type="text" id="album" name="album"  tabindex="4"/><br><BR>
  <input type="file" accept="audio/*" name="file1" id="file1"><br><BR>
  <input type="button" value="Upload File" onclick="uploadFile()"><BR>
  <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
  <font color="#FF0000"><h4  id="status"></h4></font>
  <font color="#0099FF"><p id="loaded_n_total"></p></font>
</form>
</body>
</html>
Posted
Updated 23-Feb-14 19:50pm
v2

1 solution

You can use below function in javascript:

C#
function parseURLParams(url) {
    var queryStart = url.indexOf("?") + 1,
        queryEnd   = url.indexOf("#") + 1 || url.length + 1,
        query = url.slice(queryStart, queryEnd - 1),
        pairs = query.replace(/\+/g, " ").split("&"),
        parms = {}, i, n, v, nv;

    if (query === url || query === "") {
        return;
    }

    for (i = 0; i < pairs.length; i++) {
        nv = pairs[i].split("=");
        n = decodeURIComponent(nv[0]);
        v = decodeURIComponent(nv[1]);

        if (!parms.hasOwnProperty(n)) {
            parms[n] = [];
        }

        parms[n].push(nv.length === 2 ? v : null);
    }
    return parms;
}

Use this as:
C#
var urlString = "http://www.SomeUrl.com/bar?a=a+a&b%20b=b&c=1&c=2&d#hash";
    urlParams = parseURLParams(urlString);

that will return a an object
{
  "a"  : ["a a"],     /*return arrays */
  "b b": ["b"],       /* names having special chars */
  "c"  : ["1", "2"]   /* an URL occurs multiple times */
  "d"  : [null]       /* parameters without values set to null  */ 
} 

Thereby,
parseURLParams("www.YourUrl?name=something")

Results
{name: ["something"]}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900