MyBB Community Forums

Full Version: Add MyAlerts number of alert with if condition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I know for most of the users I think this will be pretty easy, but some people like me it's so hard to make things like this to happen Undecided

Preview:

[Image: 8ZXI8.jpg]
Note that the circle behind the number is my own class, so make yours and make a beauty custom notification Smile

Version 2.0:
You can also use this alternative script working with DOMContentLoaded, works way faster, I recommend this one. (Thanks @Leefish for the tip about using DOM for faster script loading)


1.- Add wherever you want the number to show up:
<span id="showalert"></span></div>

2.- Add below the code you added on step 1:
<script>
var showalert = document.getElementById("showalert");

function Alert(func)
{
    window.addEventListener('DOMContentLoaded', func, false);
}
{
if ({$mybb->user['unreadAlerts']} != 0) {
showalert.className = notif.className + "Insert the class you want to give the element";
showalert.innerHTML = "{$mybb->user['unreadAlerts']}";}
}
</script>


--------------------------------------------------------------------

Version 1.0:

1.- Add this code to top of header_welcomeblock_member (or any other place you want to show the number of alerts):
<body onload=Alert()>

2.- Add wherever you want the number to show up:
<span id="showalert"></span></div>

3.- Add below the code you added on step 2:
<script>
var showalert = document.getElementById("showalert");
function Alert() 
{
if ({$mybb->user['unreadAlerts']} != 0) {
showalert.className = notif.className + "Insert the class you want to give the element";
showalert.innerHTML = "{$mybb->user['unreadAlerts']}";}
}
</script>

---------------------------------------------------------------

Comments for v1 and v2:
The last code as you can see only will show up if the numbers of alerts is different that cero (!=0), you can always change it for anything you want

I hope this works fine for you, and wish to help anybody that had problems trying to do this Smile
That is very cool - I use template conditionals to achieve a similar effect but that is a nice alternative.



Use the DOM ready listener rather than body.onload - that way you can put all the script together in one neat block
(2014-05-25, 01:58 AM)Leefish Wrote: [ -> ]That is very cool - I use template conditionals to achieve a similar effect but that is a nice alternative.



Use the DOM ready listener rather than body.onload - that way you can put all the script together in one neat block

What is that thing about DOM ready listener? Sorry I don't know what that is, I will try to find out too! Any help is appreciated Smile

----------------------------
Edit: Learned about what you meant, works way faster Wink !! Thanks for the tip

Edit2: Also updated tutorial including DOM for new comers Wink
Great - thank you. Do you think this might work for unread pms as well?
(2014-05-25, 09:03 PM)Leefish Wrote: [ -> ]Great - thank you. Do you think this might work for unread pms as well?

Haven't checked into it... well the MyAlerts itself have PM alert haha...

If PM do have a variable like the one on MyAlerts, it will totally work Toungue
btw tested this for 2 days, fully working no bugs ^_^

PS: Soon making another tut on how to display a modal popup if user have X field not filled, so they must fill the field to disable the popup Big Grin