GuitarGod Posted April 29, 2009 Share Posted April 29, 2009 Hi guys, Well, I have a basic templating system in which a HTML file is simply included, and that HTML file contains some PHP. mypage.htm: <td>Your name is: <?= $name; ?></td> mypage.php: <?php $name = 'John'; include( 'mypage.htm' ); ?> One of my HTML files links to a javascript file: <script src="myscripts.js"></script> The trouble is, any PHP code within myscripts.js is not parsed. Is there a way round this? It works when I simply paste the functions from myscripts.js into the HTML template, but I really want to avoid this. Any help is, as always, greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/ Share on other sites More sharing options...
ignace Posted April 29, 2009 Share Posted April 29, 2009 myscripts.js.php Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-821780 Share on other sites More sharing options...
premiso Posted April 29, 2009 Share Posted April 29, 2009 myscripts.js.php To elaborate. You need to set it as a .php extension and I believe you should also send the header content-type as text/javascript since that is the type of file you are dishing out. It is "not" required to send the content-type. But I just like to Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-821781 Share on other sites More sharing options...
Ken2k7 Posted April 29, 2009 Share Posted April 29, 2009 What page is myscripts.js? Can you post what you have for myscripts.js? Or did you mean to say mypage.php? You can use a PHP extension for scripts. But the catch is that your PHP file has to output JavaScript code! For example: mypage.php $e = 'Welcome John!'; echo 'document.write("' . $e . '");'; index.html <script type="text/javascript" src="path/to/mypage.php"></script> That should work. Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-821785 Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2009 Share Posted April 29, 2009 There is nothing magical about the file extension .js - <script type="text/javascript" src="myscripts.php"></script> Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-821807 Share on other sites More sharing options...
GuitarGod Posted April 30, 2009 Author Share Posted April 30, 2009 Hi, What I mean is, throughout my site, there are language variables (as the site is multi-lingual), for example, in index.htm I could have <?= $lang->Hello; ?>, and it works fine. But the language variables in myscripts.js don't work; I could have alert('<?= $lang->Hello; ?>') but it doesn't work. So I need a way of parsing all the language variables in myscripts.js. I tried changing the extension to php, but it didn't work. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-822589 Share on other sites More sharing options...
PFMaBiSmAd Posted April 30, 2009 Share Posted April 30, 2009 External javascript files are requested/fetched by the browser. If you have php code in them, that php code would need to have access to the php variables you are trying to use at the time the file is requested/fetched. Best I can tell is that you are setting these php variables in the php code on your main page. If you expect php variables that are set in your maiin php file to be available in javascript on that page, you would need to instead use a php include to get that external javascript into the main php page and make it inline javascript. If you must use an external javascript file, you will need to either put the variables into session variables or separate out the variables into their own include file. Then the external javascript file would either resume the session or include the file with them in order to gain access to the values. Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-822601 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 In your JavaScript file (which should have the extension .php and *NOT* .js). Unless you told your server to treat .js files as .php, PHP won't run on it. In your now .php file, include the class lang or whatever it's called and use it. It should work now. And you can use it as an external JS file as I've mentioned in a previous post in this topic. Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-822608 Share on other sites More sharing options...
GuitarGod Posted May 1, 2009 Author Share Posted May 1, 2009 Hi, Sorry to be a pain. I tried including the lang class, but then the javascript didn't work. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-823497 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 Post what you have. Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-823500 Share on other sites More sharing options...
GuitarGod Posted May 1, 2009 Author Share Posted May 1, 2009 Index.php <?php // Lots of irrelevant code here // include( 'index.htm' ); ?> index.htm <html> <title> ... etc <script language="javascript" type="text/javascript" src="scripts.js.php"></script> scripts.js.php <? include( 'language.php' ); ?> function somefunction() { alert( '<?= $lang->Hello; ?>' ); } language.php <?php $lang = new stdClass(); $lang->Hello = 'Hi'; ?> Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-823506 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 I'm guessing $Hello is a public field in stdClass? Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-823518 Share on other sites More sharing options...
GuitarGod Posted May 1, 2009 Author Share Posted May 1, 2009 I think it is, it's just a variable after all isn't it? Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-823530 Share on other sites More sharing options...
PFMaBiSmAd Posted May 1, 2009 Share Posted May 1, 2009 Your problem is likely because of the lazy-way short open tags that are being used in scripts.js.php Change all <? to <?php and <?= to <?php echo Edit: In fact, making that change causes the alert box to work for the code you posted. More wasted time caused by short open tags. Don't use short open tags, ever. Sigh... Quote Link to comment https://forums.phpfreaks.com/topic/156110-help-on-parsing-external-javascript/#findComment-823539 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.