0
xhr.send("from=guest&msg=" + document.getElementById("msg").value)

Instead of guest I want to get the name of the logged in user from the query string. Where guest is above should be the username lee as below. How can I change this? thanks

/index.html?username=lee&password=abc#

(no jquery)

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Barmar Jan 15 '14 at 13:09

1 Answers1

0

You'll need a helper function like this:

function qs(search_for) {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0  && search_for == parms[i].substring(0,pos)) {
            return parms[i].substring(pos+1);;
        }
    }
    return "";
}

and you use it like this:

xhr.send("from="+ qs('username') +"&msg=" + document.getElementById("msg").value)

Why are you doing it like this anyway? username and password on query string seems really insecure / bad idea.

Latheesan
  • 23,247
  • 32
  • 107
  • 201