inherit
139476
0
Jan 19, 2020 4:24:42 GMT -8
Auburn
I will steal your smile and treasure it
263
April 2009
auburneye
|
Post by Auburn on Jan 29, 2011 9:31:04 GMT -8
So I'm slowly trying to learn to make my own codes - halfhazardly (i think it's more exciting learning as you go than from a book) - and I know this question is probably elementary, but...
How can I grab the username from someone's view-profile page and have it set as a variable?
(i have a guestimation of it but can't quite get it right)
|
|
Former Member
inherit
guest@proboards.com
77595
0
Nov 29, 2024 11:47:23 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Jan 29, 2011 10:12:14 GMT -8
Are you talking about on ProBoards forums? There is already a javascript variable called pb_username that contains the username. Is that what you mean? If not, could you expand a little bit on what you're asking for?
|
|
inherit
139476
0
Jan 19, 2020 4:24:42 GMT -8
Auburn
I will steal your smile and treasure it
263
April 2009
auburneye
|
Post by Auburn on Jan 29, 2011 10:15:33 GMT -8
Yep, on proboards. And no... because if i'm not mistaken the pb_username would be the username of whoever is logged in - - not the username of the person's profile s/he is viewing. I hope that makes more sense.
|
|
Former Member
inherit
guest@proboards.com
77595
0
Nov 29, 2024 11:47:23 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Jan 29, 2011 10:56:51 GMT -8
Ah, I see what you mean. I've not used it with ProBoards before but you can use this small code to get it. Put this in your Main Header: <script type="text/javascript"> (function(){ // Import GET Vars document.$_GET = []; var urlHalves = String(document.location).split('?'); if(urlHalves[1]){ var urlVars = urlHalves[1].split('&'); for(var i=0; i<=(urlVars.length); i++){ if(urlVars[i]){ var urlVarPair = urlVars[i].split('='); document.$_GET[urlVarPair[0]] = urlVarPair[1]; } } } })(); if(document.$_GET['user'] && location.href.match('viewprofile')) var viewprofile_username = document.$_GET['user']; </script> Those last two lines are storing the username as viewprofile_username. I'm not sure what else you want to do with it after that. If anyone has a better way to do this (which I'm sure someone does ) feel free to take over.
|
|
inherit
139476
0
Jan 19, 2020 4:24:42 GMT -8
Auburn
I will steal your smile and treasure it
263
April 2009
auburneye
|
Post by Auburn on Jan 29, 2011 11:13:58 GMT -8
Oh, that's an interesting approach, Though I don't see how that code would actually find the user itself in order to convert it to a variable. I was thinking more along the lines of grabbing it from the mini-profile. Like, grabbing the <a> and cutting it at viewprofile&user= to retrieve the username. Something sorta like.. if(location.href.match(/on=viewpr/)){ var td = document.getElementsByTagName('td'); for (i=0; i<iTd.length; i++) { if(td.className.match(/windowbg(2)?/i) && td.width == '20%' && td.vAlign == 'top' && td.innerHTML.match(/Posts:/i)){ name=td.getElementsByTagName('a'); name_cut = name.href.match(viewprofile&user......
..but I have no clue if that's at all correct. I don't know enough javascript to do this properly, lol.
|
|
Former Member
inherit
guest@proboards.com
77595
0
Nov 29, 2024 11:47:23 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Jan 29, 2011 11:25:51 GMT -8
The one I posted grabs the username from the URL of the view profile page. So from the URL /index.cgi?action=viewprofile&user=USERNAME it pulls USERNAME out. Does that method not do what you're trying to accomplish?
|
|
inherit
139476
0
Jan 19, 2020 4:24:42 GMT -8
Auburn
I will steal your smile and treasure it
263
April 2009
auburneye
|
Post by Auburn on Jan 29, 2011 11:32:32 GMT -8
oh!! *facepalm*
I see ^^ yes.. yes that's just what i was looking for. And lol, that is a much more direct approach. Thank you. Thank you.
|
|
Former Member
inherit
guest@proboards.com
77595
0
Nov 29, 2024 11:47:23 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Jan 29, 2011 11:34:30 GMT -8
Haha, you're welcome.
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Jan 29, 2011 12:06:57 GMT -8
Here's a simpler way of doing it that you may understand.
var username = location.href.split("=")[2];
This is how you would want to use it on your forum.
// If we are on the view profile page if(pb_action == "viewprofile") { // Split the URL into three parts, and grab the third section // http://support.proboards.com/index.cgi?action=viewprofile&user=jab2 var username = location.href.split("=")[2]; }
However, you may notice that if you click the "Profile" button in the menu bar it will take you to your own profile, but the "user=username" part is not included in the URL. Because of this, the username variable will be set to undefined when you try the code above as well as in the code Ryan showed you. So, we need to then search for the username in the HTML like you were doing before.
if(pb_action == "viewprofile") { var username = location.href.split("=")[2]; // If the username field isn't in the URL if(username === undefined) {
// Grab an array of all anchor tags on the page var a = document.getElementsByTagName("a");
// Loop through all these anchor tags for(var x = 0; x < a.length; x++) {
// If the current anchor tag has a URL which matches the following, // then store the text located in the parenthesis since its the username if(a[x].href.match(/action=viewprofile&user=(.+)/i)) {
// Store the matched text from the regular expression username = RegExp.$1;
// Stop the loop since there's no point in going on break; } } }
alert(username); }
|
|
inherit
139476
0
Jan 19, 2020 4:24:42 GMT -8
Auburn
I will steal your smile and treasure it
263
April 2009
auburneye
|
Post by Auburn on Jan 29, 2011 13:28:01 GMT -8
*grins* I was just wondering about the undefined. I think I'll go with that one better (though much thanks Ryan! I'll definitely store that code for future use). Also, thank you Jordan for the comments/explanations to each line, that makes a lot more sense to me now. So here's more or less what I'm trying to do: <script type="text/javascript"> /* Profile Private Message */
if(pb_action == "viewprofile" && pb_username!='Guest') { var username = location.href.split("=")[2]; if(username === undefined) { var a = document.getElementsByTagName("a"); for(var x = 0; x < a.length; x++) { if(a[x].href.match(/action=viewprofile&user=(.+)/i)) { username = RegExp.$1; break; } } } document.write('<table width="92%" height="100" cellspacing="1" cellpadding="4" class="bordercolor" align="center"><tr><td class="titlebg" height="16">Personal Message:</td></tr><tr><td class="windowbg2" valign="top" align="center"><br><form action="/index.cgi" method="post" name="postForm" enctype="multipart/form-data" onSubmit="disable(this)" style="display: inline"><input type="hidden" name="action" value="pmsend2"><input type="hidden" name="id" value=""><input type="hidden" name="subject" value="Profile Message"/><input type="hidden" name="to" value="'+username+'"/><textarea onKeyPress="javascript: countChars();" onMouseUp="javascript: countChars();" onMouseDown="javascript: countChars();" onKeyUp="javascript: countChars();" onKeyDown="javascript: countChars();"name="message" rows="7" cols="120" wrap="soft" tabindex="3"></textarea><br><input type="hidden" name="nextaction" value="post"><input type="submit" value="Send Message" onClick="document.postForm.nextaction.value=post;" accesskey="s" tabindex="4"> <input type="submit" value="Preview" onClick="document.postForm.nextaction.value=preview;" accesskey="p"></form></td></tr></table>');} </script> So basically the idea is to have a sort of quick-private-message box in a member's profile page. It all seems to be working great thus far, though I'm still trying to figure out how to make the Preview button work - it seems to just auto-submit the comment at this point..
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Jan 29, 2011 17:19:56 GMT -8
Add the red.
onClick="document.postForm.nextaction.value='preview';"
There is no variable "preview" declared anywhere, and plus the value is supposed to be a string.
|
|
inherit
139476
0
Jan 19, 2020 4:24:42 GMT -8
Auburn
I will steal your smile and treasure it
263
April 2009
auburneye
|
Post by Auburn on Jan 29, 2011 23:48:12 GMT -8
Ah yes, I originally took them off because they broke document.write(' '); but now I've tried it like so: onClick=document.postForm.nextaction.value="preview"; and it seems to function on both FF and IE. It's probably not proper but it works.. ^^;
Thanks much Jordan.
|
|
inherit
134992
0
May 26, 2012 2:38:57 GMT -8
SubDevo
Creator of LSD...
3,861
December 2008
subdevo
|
Post by SubDevo on Jan 30, 2011 0:48:31 GMT -8
You could also grab the username from the end of the Send Personal Message link href.
I guess this code would be nice for the coolness factor... but since a user is viewing the profile, and there is a "Send PM" link there and in the mini-profile, it is only one more click to send a PM. I don't see the need. But that is just me. lol Either way, it is a good code exercise.
EDIT: Heck, I'll throw one in...
if(pb_action=="viewprofile"){ var n=location.href,username; if(!/=viewprofile$/.test(n)){username=n.slice(n.lastIndexOf("=")+1);} if(username){ DO STUFF } }
|
|
inherit
139476
0
Jan 19, 2020 4:24:42 GMT -8
Auburn
I will steal your smile and treasure it
263
April 2009
auburneye
|
Post by Auburn on Jan 30, 2011 1:30:50 GMT -8
Yea ^^ it's definitely not a necessity, but pretty much just to make things more userfriendly(/lazyfriendly?). It saves a click, and also having to enter a title. I suck at coming up with titles for pms sometimes.
(I also sense it makes it more interactive, and inviting of communication. It's a lot more "casual" than a pm - and can be used for something simple like "i really liked that post you made" or "hey, come to the chat?". It's hard to explain, lol.. )
But the other reason is simply to add some flavor to the profile page. There are very few codes or customization options that i've seen for the user profile, and I wanna make it more shnazzy if I can.
|
|
inherit
134992
0
May 26, 2012 2:38:57 GMT -8
SubDevo
Creator of LSD...
3,861
December 2008
subdevo
|
Post by SubDevo on Jan 30, 2011 1:39:23 GMT -8
Yea, I'm ok with that. Did you happen to see my edit? No loops. I'll explain the code if you wish...
|
|