Destramic Posted October 30, 2014 Share Posted October 30, 2014 when tyring to pass a variable to a extenal javascript file it just shows up as a sting and the variable isnt beening executed var timezone_offset = <?= $timezone_offset ?>; im guessing its probably down to the fact the variable is being passed from my framework controller... so im wondering how do you send a variable to a external javascript file when using a framework? $this->view->timezone_offset = "test"; // in my controller // passing varibales to template in my view if (preg_match("/\.html$/i", $file_name)) { extract($this->_variables); require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name; } thanks guys Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 30, 2014 Share Posted October 30, 2014 it just shows up as a sting exactly what does the 'view source' of the page show for the whole line where the php variable is at? Quote Link to comment Share on other sites More sharing options...
Destramic Posted October 30, 2014 Author Share Posted October 30, 2014 well the php variable is in the extental js file which is included in the head...is this what your asking for? <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" /> <script src="http://127.0.0.1/bisi/media/js/library/jquery/2.1.1/jquery.min.js"></script> <script src="http://127.0.0.1/bisi/media/js/library/jqueryui/1.11.2/jquery-ui.min.js"></script> <script src="http://127.0.0.1/bisi/media/js/scripts/global.js"></script> // here is the line of the file where php var is in <script src="http://127.0.0.1/bisi/media/js/scripts/user_timezone.js"></script> <link rel="stylesheet" type="text/css" href="http://127.0.0.1/bisi/media/css/items/items.css" /> <title>BiSi</title> </head> Quote Link to comment Share on other sites More sharing options...
Destramic Posted October 30, 2014 Author Share Posted October 30, 2014 just shows up as var timezone_offset = <?= $timezone_offset ?>; the php variable isnt being executed Quote Link to comment Share on other sites More sharing options...
Barand Posted October 30, 2014 Share Posted October 30, 2014 (edited) My theory is that by the time the external js file is loaded by the browser, PHP has finished executing on the server. edit: try issuing an AJAX call to get the value when the page has loaded Edited October 30, 2014 by Barand Quote Link to comment Share on other sites More sharing options...
Destramic Posted October 30, 2014 Author Share Posted October 30, 2014 but the variables are extracted first before the template is included...so when rendering the template my variable should be accessable to the js file =/ Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 30, 2014 Share Posted October 30, 2014 php code is normally only parsed when in a .php file, not a .js file. Quote Link to comment Share on other sites More sharing options...
Destramic Posted October 30, 2014 Author Share Posted October 30, 2014 i tested by putting <?php $test="hello"; ?> in my template file above where the script get loaded... when tyring to see if i could get the variable value in the script...no luck just doesnt make sense Quote Link to comment Share on other sites More sharing options...
Destramic Posted October 30, 2014 Author Share Posted October 30, 2014 i think the only way to put a php var in js is if the script is internal and not external Quote Link to comment Share on other sites More sharing options...
Barand Posted October 30, 2014 Share Posted October 30, 2014 Yes, that way it gets rendered in the the js script while the PHP is executing on the server. The page is then sent to the browser at which point the external JS file is included Quote Link to comment Share on other sites More sharing options...
Destramic Posted October 30, 2014 Author Share Posted October 30, 2014 well i did a simple test and still did get the variable passed accross...but didnt work...i dunno what im doing wrong <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <?php $timezone_offset = "test"; ?> <script> var time = "test"; </script> <script src="global.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </body> </html> global.js alert(time); var timezone_offset = "<?php echo $timezone_offset; ?>"; alert(timezone_offset); Quote Link to comment Share on other sites More sharing options...
Barand Posted October 31, 2014 Share Posted October 31, 2014 Ajax method Test3.js var testvar = 'Failure'; function checkVar() { alert(testvar); } Test1.html <html> <head> <script type="text/javascript" src="test3.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> $().ready(function() { $("#btnTest").click(function() { checkVar(); }) $.get( "test2.php", function(data){ testvar = data; }, "text" ) }) </script> </head> <body> <input type="button" name="btnTest" id="btnTest" value="Test Var"> </body> </html> Test2.php <?php echo "Success"; ?> Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 31, 2014 Share Posted October 31, 2014 You could in fact use .htaccess to parse the .js file as PHP, so it will run the file and parse any variables etc. <Files ~ '^(global.js)> ForceType application/x-httpd-php </Files> Quote Link to comment Share on other sites More sharing options...
Destramic Posted November 1, 2014 Author Share Posted November 1, 2014 is that safe to do that? Quote Link to comment Share on other sites More sharing options...
Alex_ Posted November 1, 2014 Share Posted November 1, 2014 Assuming the PHP Script is run on the same page you're loading, and above the actual <script> import tag, you could make it easy on yourself. A lot of applications use their own global object to keep some form of data that is fairly unionized throughout more than one page. A way you can do this is something like this: <script> MyApplication = { meta: {} }; //Global object MyApplication. For this example, with 'meta' MyApplication.meta.MyVariable = '<?php echo $timezone_offset;?>'; </script> <script src="path/myJsFile.js"></script> And then in your external JS file you do var myVariable = MyApplication.meta.MyVariable; And you have access to it. http://i.imgur.com/VKUBfLm.png You can also do this if you have an array of items you want accessible in the frontend, by doing something like this.. <script> MyApplication = { meta: {} }; <?php foreach($myList as $key => $value) { echo "MyApplication.meta." . $key . " = '".$value."';"; } ?> </script> Hope it helps. Quote Link to comment Share on other sites More sharing options...
Destramic Posted November 1, 2014 Author Share Posted November 1, 2014 lovely!, thanks alot for your help guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.