News:

Wondering if this will always be free?  See why free is better.

Main Menu

SMF settings in JavaScript file

Started by Adrek, March 04, 2012, 04:54:03 AM

Previous topic - Next topic

Adrek

Hello.

How can I add settings in JS file? Is this even possible?
Polskie wsparcie SMF na simplemachines.org

the simplest solution is most likely the right one

DanCarroll


Adrek

I want to use $modSettings['some_setting'] in JS file, don't know what more can i say
Polskie wsparcie SMF na simplemachines.org

the simplest solution is most likely the right one

NanoSector

#3
That's impossible to do with PHP arrays and JS without breaking things.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Joker™

You just need to escape the strings properly to make it work. AFAIR, try to go through template file of "Personal Message".

P.S - Try writing inline JS first and then move it to file ;).
Github Profile
Android apps
Medium

How to enable Post Moderation

"For the wise man looks into space and he knows there is no limited dimensions." - Laozi

All support seeking PM's get microwaved

MrPhil

Quote from: phantomm on March 04, 2012, 07:06:41 AM
I want to use $modSettings['some_setting'] in JS file, don't know what more can i say
PHP exists only on the server machine. None of it is available on the client machine (browser). Javascript is executed (run) only on the client machine.

There is nothing to prevent you from writing out a PHP value as a variable within a <script> section:

<script type="text/javascript">
mysetting = 'ABCDEF';
.... other code
</script>

which could be produced by

print '<script type="text/javascript">';
print "mysetting = '".$modSettings['some_setting']."';";
.... other code
print '</script>';


It's a one-way trip from the server to the browser, so you can't modify that setting and send it back to the server, at least not without a form or URL Query String.

Matthew K.

Quote from: Yoshi2889 on March 04, 2012, 07:25:46 AM
That's impossible to do with PHP and JS without breaking things.
Uh...no it's not.

NanoSector

Quote from: Labradoodle-360 on March 04, 2012, 05:41:02 PM
Quote from: Yoshi2889 on March 04, 2012, 07:25:46 AM
That's impossible to do with PHP and JS without breaking things.
Uh...no it's not.
I meant to say "arrays", my bad :P
It's indeed possible with just strings.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Matthew K.

It's not impossible with arrays either, dude.

NanoSector

Quote from: Labradoodle-360 on March 05, 2012, 12:48:11 PM
It's not impossible with arrays either, dude.
Maybe with the javascriptescape function but it always breaks my code if I try to do so ::)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Matthew K.

Nope. JavaScriptEscape(); is an SMF originated PHP function that escapes all PHP string variables with the appropriate escaped versions to be thrown into JavaScript.

json_encode(); and json_decode(); convert PHP arrays to JavaScript or vise versa in the case of json_decode.

Adrek

Polskie wsparcie SMF na simplemachines.org

the simplest solution is most likely the right one

Marcus Forsberg

Hey there,

Have you managed to solve this? It's extremely simple using MrPhil's method. Just have a look at the top of index.template.php where SMF adds a couple of variables for JS to use. If you need no more help, please do mark this topic solved! :)

Advertisement: