MyBB Community Forums

Full Version: JS - get username
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How would I use jquery/javascript to get the logged in users username?
Why can't you just use {$mybb->user['username']}? What are you trying to do?
(2013-03-25, 12:10 AM)Fábio Maia Wrote: [ -> ]Why can't you just use {$mybb->user['username']}? What are you trying to do?

Make a custom userscript.
The username is displayed on all forum pages inside the welcome block (in most themes anyway). If you're able to rely on that, you can grab the username quite easily:

var username = document.querySelector('#panel strong a').innerHTML;

Or using jQuery:

var username = $('#panel strong a').html();

This assumes a default MyBB installation.
(2013-03-25, 12:54 AM)Fábio Maia Wrote: [ -> ]The username is displayed on all forum pages inside the welcome block (in most themes anyway). If you're able to rely on that, you can grab the username quite easily:

var username = document.querySelector('#panel strong a').innerHTML;

Or using jQuery:

var username = $('#panel strong a').html();

This assumes a default MyBB installation.


TypeError: Cannot read property 'innerHTML' of null
get stack: function () { [native code] }
message: "Cannot read property 'innerHTML' of null"
set stack: function () { [native code] }
__proto__: Error


Figured it out.
var myname = $('div.welcome strong a span strong').html();
Like I said, the code assumed a default installation. Your theme is different, hence why the selector returned null. Glad you figured it out yourself. Wink