I am wondering, is there a template, that when put on a blog (or anywhere), displays the username of whoever clicked on the blog (or wherever the template was placed)?
I am wondering, is there a template, that when put on a blog (or anywhere), displays the username of whoever clicked on the blog (or wherever the template was placed)?
I'd actually like some help too, if that's alright with you lynque :P
I'm wondering if the following combination would make a working button on Wikia:
The Wikitext in the article:
<span class="button insertusername" onclick="function UserNameReplace()">Insert text for button to display username of reader</span>
Javascript:
function UserNameReplace() { if(typeof(disableUsernameReplace) != 'undefined' && disableUsernameReplace || wgUserName == null) return; $("span.insertusername").html(wgUserName); } addOnloadHook(UserNameReplace);
Edit: apparently, that DOESN'T work the way I want. Back to thd drawing board...
That's just using the javascript in the link combined with something I saw on w3schools though -- not too sure how correct that is. I'm also wondering if there's a way of putting the "onclick" bit into the javascript instead of the HTML (saw something about ".object", but I've yet to get my head around that one).
That would create a span with the class "insertusername," so everything within the span would be replaced with your username. The "onclick" function happens when you click the span element.
So, no "function" in the onclick event, just call the function you want like onclick="javascript:UserNameReplace();"
<span class="button insertusername" onclick="javascript:UserNameReplace();">Insert text for button to display username of reader</span>
…and since it's a span with class="insertusername" you get a button like…
…or like
…if you're not logged in.
If you wanted the username replacement to wait until you clicked the button, you would have to edit the javascript, removing the last line, "addOnloadHook(UserNameReplace);" which is executing the UserNameReplace() function "on load." Then you make a button to call the script like this:
<span class="button insertusername" onclick="javascript:UserNameReplace();">Replace user name.</span>
So you would see
and then click it and it would turn into…BUT… you have to put this span into a MediaWiki page and then place it with <verbatim> tags in order to make the javascript onclick event work. Otherwise it will be stripped when the page is generated. The reason for this is… well, you don't want just any anonymous user to be able to put javascript links into a page 😃
…I decided to make sure I actually know what I'm talking about and made a working copy.
This would work fine too, and it won't require a custom MediaWiki page.
$( '.insertusername' ).on( 'click', function () { if ( mw.config.get( 'wgUserName' ) ) { $( this ).html( mw.config.get( 'wgUserName' ) ); } } );
$( '.insertusername' ).on( 'click', function () { if ( mw.config.get( 'wgUserName' ) ) { $( this ).html( mw.config.get( 'wgUserName' ) ); } } );
hmmm... neat :D
Javascript is still mysterious magic to me sometimes 😅
Yeah, I need more help with it. Coding isn't my strength, so maybe tell me how to do it in the simplest way possible?
First off: this will only work the way you want if you are an admin on your wiki. If not, it's possible to make this work for yourself only -- no-one else looking at your blog will see the text changing to their username.
I'll refer to Fubuki's code, since Lynque's instructions involve editing certain bits of the one I provided (much appreciated, but guessing Coolboy87 may find it a little confusing at this point).
The first thing you need to do is to copy Fubuki's javascript sample (the colourful text with random $ signs and ( ) brackets all over the place). You need to paste that into your wiki's MediaWiki:Common.js file (if it's never been edited before, it'll say something along the lines of "This page needs to be created"). Doing this "installs" your desired function to your wiki.
Once you've done that, applying class="insertusername"
to anything on a page should cause it to show the username of whoever's looking at the page. The easiest way to do that is to copy the following:
<span class="insertusername">Clicking this text will show username</span>
The thing above won't generate a button, but it WILL change the text if it's clicked on. If you want a button, change class="insertusername"
to class="button insertusername"
That's probably about as simple as I can explain it. You can get a little fancier with it, but that may be better explored later when you can see how some of the code resembles HTML.
Many thanks for the crash course, Lynque and Fubuki -- I'm finding javascript a little harder to crack open compared to CSS ^^;
Okay, I have copied the code and put it on my wikia's Common js and I have copied the other thing, so hope it works!
Yay it worked!