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 2:08:27 GMT -8
Yes, please do explain if you can. ^^; It'll help me learn better.
|
|
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 3:07:59 GMT -8
Ok, sure thing. if(pb_action=="viewprofile"){ var n=location.href,username; if(!/=viewprofile$/i.test(n)){username=n.slice(n.lastIndexOf("=")+1);} if(username){ DO STUFF } }First we test if we are on the profile page. if(pb_action=="viewprofile"){I put the location.href in a variable to help make the code smaller. Also declared the variable "username" on the same line. var n=location.href,username;Since we are on a profile page, the location.href can only end in two things. Either "=viewprofile" or "=viewprofile&user=USERNAME". In the following line we test to see if the location.href has ONLY "=viewprofile" at the end and nothing following it. if(!/=viewprofile$/.test(n)){If the href doesn't end with "=viewprofile" then we know it MUST be "=viewprofile&user=USERNAME". Since the username will be at the end, we can just slice the href string at the last position of "=". Anything after the last "=" will be the username. slice does what is says. It slices a portion of a string. You need to tell it what position to start and end. slice(start,end) So we use lastIndexOf to return the last position of a character in the string. n.lastIndexOf("=") will return the last position of "=". Add 1 to it to skip the actual "=" sign. n.lastIndexOf("=")+1 Now we have the position of the first letter of the username. Use that index as the start in the slice. username=n.slice(n.lastIndexOf("=")+1);Since we don't tell slice where to end, it gives us everything else to the end of the string. And of course, we set username equal to the result. Now we just test to see if username has a value or not. And continue... if(username){ DO STUFF }Regards, SubDevo EDIT:To make this code more bullet-proof, add the Blue to the code. Guests can't send PMs so no need to show the box on the Profile page. <script type="text/javascript"> if(pb_action=="viewprofile"&&pb_username!="Guest"){ var n=location.href,username; if(!/=viewprofile$/i.test(n)){username=n.slice(n.lastIndexOf("=")+1);} if(username){ DO STUFF } } </script>
|
|
#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 30, 2011 11:29:23 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. Glad to help. By the way, the proper way would be to escape the ' characters. onClick="document.postForm.nextaction.value=\'preview\';"Also, I just realized that the name is always in the page title so it would actually be better to just do something like... if(document.title.match(/View\sProfile:\s(.+?)\s/)) { username = RegExp.$1; }Then you wouldn't have to search through the DOM if the URL doesn't contain the username. You could also grab the username from the end of the Send Personal Message link href. That's what the other codes posted in the thread do by the way.
|
|
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 11:43:50 GMT -8
Yup, title is a good idea... heck, so many ways to accomplish the same goal! I love it...
|
|
#00AF33
Official Code Helper
19529
0
1
Nov 19, 2012 14:18:28 GMT -8
Todge
**
17,324
January 2004
todge
|
Post by Todge on Jan 30, 2011 15:03:40 GMT -8
The name in the page title is the display name, not the username. I'd go something like... if(pb_action == 'viewprofile' && location.href.match(/&user=/)){ var username = location.href.split(/&user=/)[1];} else if(pb_action == 'viewprofile'){ var username = pb_username;} Pretty much what Sub did, but grabbed the username if you are viewing your own profile too.
|
|
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 15:29:30 GMT -8
Ahh title=displayname. geez, I knew that. Just didn't question it...
And yea, split. Good idea. Better than slice in this case.
if(pb_action=="viewprofile"){ var username=location.href.split(/&user=/i)[1]; if(username){ DO STUFF } }
|
|
#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 30, 2011 17:03:57 GMT -8
Bah, can't believe I didn't realize that. Using the pb_username variable should have been obvious too...
|
|
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 17:46:34 GMT -8
Woah, epic replies. =o I have a question. So then to make that code work for all except guests, would setting up this way work? if(pb_action == 'viewprofile' && location.href.match(/&user=/) && pb_username!='Guest'){ var username = location.href.split(/&user=/)[1];} else if(pb_action == 'viewprofile' && pb_username!='Guest'){ var username = pb_username;} Though when I look at that, something seems off. I think maybe this would be better? if(pb_action == 'viewprofile' && location.href.match(/&user=/)){ var username = location.href.split(/&user=/)[1];} else if(pb_action == 'viewprofile'){ var username = pb_username;}
if(pb_username!='Guest'){ document.write(' <the table> ');}
Because, in the first example, the document.write is not inside an if() statement, so it would load the table anyhow but just fail to designate 'username' as anything. While the second would designate 'username' regardless of who is viewing, but only load the table if it's a member.. is that correct?
|
|
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 18:05:31 GMT -8
Yup, you are getting it.
if(pb_action=="viewprofile"&&pb_username!="Guest"){ var username=location.href.split(/&user=/i)[1]; if(username){ DO STUFF } }
If you want to test this, use alert and throw it in your global footer and view some profiles. Log out and view a profile.
<script type="text/javascript"> if(pb_action=="viewprofile"&&pb_username!="Guest"){ var username=location.href.split(/&user=/i)[1]; if(username){ alert(username); } } </script>
The only thing you should notice is that you will still get the alert if you click on your link to your profile (user=yourname). Which means that if you are writing the table (in place of the alert), you will see the pm box. If you want to avoid that, then we can add this:
<script type="text/javascript"> if(pb_action=="viewprofile"&&pb_username!="Guest"){ var username=location.href.split(/&user=/i)[1]; if(username&&username!=pb_username){ alert(username); } } </script>
|
|