Logged Out
Create an Account
Login:
Password:

Forgot your password?
Getting User Rank in Javascript

Getting User Rank in Javascript
[Back to Index]  [Bottom of Thread]
Thread Tags
Primary: [General]
Secondary: None
I ran across the need to get the user's Rank into some Javascript I was writing, and I thought I'd share it.

.
.
.
<body>
<!--System:IfRank:1--><div id="Rank1" style="display:none"></div><!--System:EndIfRank-->
<!--System:IfRank:2--><div id="Rank2" style="display:none"></div><!--System:EndIfRank-->
<!--System:IfRank:3--><div id="Rank3" style="display:none"></div><!--System:EndIfRank-->
<!--System:IfRank:4--><div id="Rank4" style="display:none"></div><!--System:EndIfRank-->
<!--System:IfRank:5--><div id="Rank5" style="display:none"></div><!--System:EndIfRank-->
<!--System:IfRank:6--><div id="Rank6" style="display:none"></div><!--System:EndIfRank-->
<!--System:IfRank:7--><div id="Rank7" style="display:none"></div><!--System:EndIfRank-->
<!--System:IfRank:8--><div id="Rank8" style="display:none"></div><!--System:EndIfRank-->
.
.
.
</body>

Javascript function to return the rank (you can make it either external or inline):

<script type="text/javascript">
function getRank()
{
var rank = 1000;
var rank1 = document.getElementById("Rank1");
var rank2 = document.getElementById("Rank2");
var rank3 = document.getElementById("Rank3");
var rank4 = document.getElementById("Rank4");
var rank5 = document.getElementById("Rank5");
var rank6 = document.getElementById("Rank6");
var rank7 = document.getElementById("Rank7");
var rank8 = document.getElementById("Rank8");

if (rank1)
{
rank = 1;
}
else if (rank2)
{
rank = 2;
}
else if (rank3)
{
rank = 3;
}
else if (rank4)
{
rank = 4;
}
else if (rank5)
{
rank = 5;
}
else if (rank6)
{
rank = 6;
}
else if (rank7)
{
rank = 7;
}
else if (rank8)
{
rank = 8;
}

return (rank);
}


(edited, somehow the message entry widget decided I was done early

Yes, there was probably an easier way to do it. Yes, I'm a programmer and I cringe at that code. It works, though.

D
just looked at your site and noticed something:

the System:Title tag isn't supposed to be there (u can see that in the system window top bar) it is part of the main content of the site and displays stuff like: News, gallery, forum (with the searchbar but i'm not sure about that)

--
designer of http://aszune.dkpsystem.com
Quote by borkjev
just looked at your site and noticed something:

the System:Title tag isn't supposed to be there (u can see that in the system window top bar) it is part of the main content of the site and displays stuff like: News, gallery, forum (with the searchbar but i'm not sure about that)


I'm sorry, I didn't follow that at all.

D
Ah, I see. Some editor replaced the left angle with "&lt;". Joy

Thanks!

D
<body>
</body>

<Script language="javascript">
var sGlobalRankID = <!--System:IfRank:1-->1<!--System:EndIfRank--><!--System:IfRank:2-->2<!--System:EndIfRank--><!--System:IfRank:3-->3<!--System:EndIfRank--><!--System:IfRank:4-->4<!--System:EndIfRank--><!--System:IfRank:5-->5<!--System:EndIfRank--><!--System:IfRank:6-->6<!--System:EndIfRank--><!--System:IfRank:7-->7<!--System:EndIfRank--><!--System:IfRank:8-->8<!--System:EndIfRank-->
</Script>

Now in any Javascript function you write, you can reference the sGlobalRankID value (as long as the Rank info is local to the same Page as the Javascript function). Such as:

<Script language="javascript">
function Show_Rank() {
alert('Your rank is:' + sGlobalRankID + '. You are super cool.');
}
</script>

[EDIT]: This uses the IfRank tag. I'm not sure if there is a direct PHP tag just for getting the Rank. (It's not on the Admin Advanced Settings notes if it DOES exist) If it does exist, it would make more sense to use it than the multiple IfRank tags.
Excellent, knew there had to be a better way. I never thought of embedding the tags inside the JS <script></script> area I write computer games, not web apps

D


[Back to Index]  [Top of Thread]