MyBB Community Forums

Full Version: Use Variable In Javascript
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to use the original MyBB variables in a javascript piece. However because they have $ in them, it messes up and completely removes the area.

var dropdownHTML = '<div style="padding: 10px; text-align: center;">'
 +'<a href="{$mybb->settings['bburl']}/private.php">Messages ({$mybb->user['pms_unread']} New)</a><div style="height: 10px;"></div><a href="{$mybb->settings['bburl']}/usercp.php?action=profile">Edit Profile</a><div style="height: 10px;"></div>{$modcplink}<div style="height: 10px;"></div>{$admincplink}<div style="height: 10px;"></div><a href="{$mybb->settings['bburl']}/member.php?action=logout&amp;logoutkey={$mybb->user['logoutkey']}">Logout</a>'
+'</div>';

So I went in and added a backslash so that it was not noticed and now it shows up but the links don't place properly, it's just text.
var dropdownHTML = '<div style="padding: 10px; text-align: center;">'
 +'<a href="{$mybb->settings['bburl']}/private.php">Messages ({$mybb->user['pms_unread']} New)</a><div style="height: 10px;"></div><a href="{$mybb->settings['bburl']}/usercp.php?action=profile">Edit Profile</a><div style="height: 10px;"></div>{\$\modcplink}<div style="height: 10px;"></div>{\$\admincplink}<div style="height: 10px;"></div><a href="{$mybb->settings['bburl']}/member.php?action=logout&amp;logoutkey={$mybb->user['logoutkey']}">Logout</a>'
+'</div>';


Any ideas on how to go about this?


And it's weird because it's only modcp and admincp link that are doing this...
Where are you putting this code? A javascript file or a template? It would work if it was in a template as that's how the other javascript variables get populated.
I'm putting it directly into the member welcome block template.
So what happens when you view the page source and look at the javascript variable value? If you escape the variables, it won't output the actual values of them.
Interestingly enough, it looks like MyBB automatically adds in the
<!-- start blah blah blah -->

Is there a way to possibly remove that? Could be whats causing the issue? This also still occurs if I remove the
 &nbsp; 
Oh, yeah, if the variables are whole templates it'll have those in.

What are you actually trying to do? Typically having a javascript variable with lots of HTML like this wouldn't be the best way to go about it, you'd have a div containing HTML you want to hide/show and toggle it rather than storing it all in a variable like this.
It's pretty much just a drop down menu, I'm using the cbox.ws code for cboxes to create it because I'm lazy as hell and don't know squat about java lol and that's how they have their code set up, I'm just recycling it for personal use.

Is there a way to possibly remove that?

If I just paste the modcp link in there it will show for everyone.