***Update: This code does not work in Firefox***
I'll start this post with a disclamer that this code is not my own, I found different parts of the following code in different blog posts around the internet, but could not find this complete solution in one coherent post, so I decided to put it up myself. I found the beginning of the code
here.
Place the following code just inside the PlaceHolderMain tag of a newform.aspx page, or on a page with any custom form on it, and it should automatically populate a people or groups field with the name of the currently logged on user.
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function fillDefaultValues() {
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
var vals = new Object(getUserDisplayName());
var assignedToInput = getTagFromIdentifierAndTitle("div", "", "People Picker");
assignedToInput.innerHTML = vals;
for (var i=0; i < args.length; i++) {
var nameVal = args[i].split("=");
var temp = unescape(nameVal[1]).split('+');
nameVal[1] = temp.join(' ');
vals[nameVal[0]] = nameVal[1];
}
}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
function getUserDisplayName() {
var tags = document.getElementsByTagName('a');
for (var i=0; i < tags.length; i++) {
if(tags[i].innerText.substr(0,7) == 'Welcome') {
return tags[i].innerText.substr(8,tags[i].innerText.length);
}
}
}
</script>