Welcome Guest, Not a member yet? Register   Sign In
Parsing php in javascript
#1

[eluser]Maglok[/eluser]
Alright this is going to get a little complicated so bear with me.

I have several subdomains each with their own index.php in those index.php's I define what skin the site uses. So skin1.domain.com and skin2.domain.com have the proper colors.

Each template has a different css. Easy enough. I use carabiner to combine css and javascript (css and js thus each being their own file) to reduce the amount of http requests.

I am now in the process of streamlining the coding. At the moment I have snippets of JS code for each script I need initiated. Since not every page needs every snippet I am moving each snippet to a javascript file of it's own which I include on a page by page basis with carabiner.

For example this is how the tinyMCE init looks like:

Code:
[removed]
        tinyMCE.init({
            mode : "specific_textareas",
            editor_selector : "editor",
            plugins : "spellchecker",
            theme : "advanced",
            theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,cut,copy,paste,|,link,unlink,anchor,|,bullist,numlist,blockquote,|,outdent,indent,|,hr,image,spellchecker",
            theme_advanced_buttons2: "",
            theme_advanced_buttons3: "",
            theme_advanced_default_background_color : "#FFFFFF",
            spellchecker_languages : "+Dutch=nl,English=en",
            content_css : "<?php echo APP_MAIN; ?>css/<?php echo APP_SITE; ?>.css",
            remove_script_host : false,
            relative_urls : false,
            save_enablewhendirty : true
        });
    [removed]

You will notice I echo APP_MAIN and APP_SITE. Those are globals that don't get parsed in the javascript file if it is not inline.

I googled around some and people seem to suggest to tell apache to parse javascript by php like so:

Code:
AddHandler application/x-httpd-php .js

This doesn't work. I am even bypassing carabiner for it at the moment. TinyMCE does initialize, but it isn't taking the php echo-ing.

I could of course create a library or controller or so that outputs the correct code snippets, but I really rather keep all of that out of CI and just in a javascript scripts dir.

Anyone follow what I am trying to achieve?
#2

[eluser]cahva[/eluser]
Well the easiest to do here would be to add a little inline js where you define APP_MAIN and APP_SITE before loading the scripts. So inside your layout/header view something like this should work:
Code:
<html>
<head>
< script>
var APP_MAIN = '<?php echo APP_MAIN ?>',
    APP_SITE = '<?php echo APP_SITE ?>';
< /script>
Now those global variables would be available in the other scripts:
Code:
content_css : APP_MAIN + "css/" + APP_SITE + ".css",
#3

[eluser]Maglok[/eluser]
Yeah that wouldn't work. Cause the whole point is to not have to load script specific code on pages that don't use the code. It is just two, I know, but there has to be a way around this.

Thanks though. Smile
#4

[eluser]eokorie[/eluser]
Hi Maglok,

I had a similar issue recently and the only way I was able to get this to work was to encode the base_url and the APPPATH into a json array and access them through javascript.

Hope that gives you an idea
#5

[eluser]Atharva[/eluser]
The workaround will be store your js code in php file something like my_js.php
Code:
[removed]
        tinyMCE.init({
            mode : "specific_textareas",
            editor_selector : "editor",
            plugins : "spellchecker",
            theme : "advanced",
            theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,cut,copy,paste,|,link,unlink,anchor,|,bullist,numlist,blockquote,|,outdent,indent,|,hr,image,spellchecker",
            theme_advanced_buttons2: "",
            theme_advanced_buttons3: "",
            theme_advanced_default_background_color : "#FFFFFF",
            spellchecker_languages : "+Dutch=nl,English=en",
            content_css : "&lt;?php echo APP_MAIN; ?&gt;css/&lt;?php echo APP_SITE; ?&gt;.css",
            remove_script_host : false,
            relative_urls : false,
            save_enablewhendirty : true
        });
    [removed]

load it with
Code:
&lt;script type="text/javascript" src="http://domain/my_js.php">&lt;/script&gt;



have you tried this?




Theme © iAndrew 2016 - Forum software by © MyBB