nevillejones Posted December 11, 2006 Share Posted December 11, 2006 First things first, thanks to anyone how reads this message, especially if you can help (it is very much appreciated).Sorry for the length of this, with my examples and notes this will be a long one.I use the online shop osCommerce and I wanted to take the JavaScript that is in the page out and put then in to external files, which will be linked.I started this work but came up with bizar problems, which seems like the php interpreter have problems understanding JavaScript. This should not be a problem, however it seems like the php is expecting html but instead gets JavaScript, now instead of ignoring the code it does not make sense of PHP alters the code which messes up the JavaScript.Here is an example (in the file test.js.php):[code]<?php header('Content-type: text/javascript'); // see: http://bugs.php.net/bug.php?id=12318&edit=1 // php seems to mess up javascript file, to resolve turn off the buffer ob_end_flush(); echo "// PHP version = " . PHP_VERSION . "\n";?>alert ("BOF:test.js.php<br>");function is_less_than (a,b) { return (a<b) ? true : false;}function is_less_or_equal_than (a,b) { return (a<=b) ? true : false;}function is_more_than (a,b) { return (a>b) ? true : false;}function is_more_or_equal_than (a,b) { return (a>=b) ? true : false;}function is_equal (a,b) { return (a==b) ? true : false;}a=1;b=2;alert("a=" + a);alert("b=" + b);if (is_less_than (a,b)) alert ("a is less than b");if (is_less_or_equal_than (a,b)) alert ("a is less or equal than b");if (is_more_than (a,b)) alert ("a is more than b");if (is_more_or_equal_than (a,b)) alert ("a is more or equal than b");if (is_equal (a,b)) alert ("a is equal to b");alert ("EOF:test.js.php<br>");[/code]This is a simple script the create some simple logical check, like less that, more that, etc. The output of this page should be the same (except for the php code at the top), once it has gone through the web server and PHP parser, however I have noticed that the line:[code]return (a<=b) ? true : false;[/code]has been changed to:[code]return (a=b) ? true : false;[/code]It all seems to be down to the '<' which PHP is trying to make sence of but it thinks it is HTML and for some reason thinks it knows best and removes it.It also seem not do anything to the first '<' but the second one is removed so the output for the first two fuctions is:[code]function is_less_than (a,b) { return (a<b) ? true : false;}function is_less_or_equal_than (a,b) { return (a=b) ? true : false;} [/code]And as you can see the output of the second function is not identical to the source.Does any one have any ideas on how I can get around this problem? I have searched loads on this topic and have read some interesting notes, which I though could help me but have not helped in the end.I know a way I can get around this but it is not good and will take allot of effort to make the changes I need to do to get this working.If you rap the JavaScript into a string which you store in a variable and echo out then this is not interpreted by php so is left untouched. eg:[code]<?php header('Content-type: text/javascript'); // see: http://bugs.php.net/bug.php?id=12318&edit=1 // php seems to mess up javascript file, to resolve turn off the buffer ob_end_flush(); echo "// PHP version = " . PHP_VERSION . "\n";echo 'alert ("BOF:test.js.php<br>");function is_less_than (a,b) { return (a<b) ? true : false;}function is_less_or_equal_than (a,b) { return (a<=b) ? true : false;}function is_more_than (a,b) { return (a>b) ? true : false;}function is_more_or_equal_than (a,b) { return (a>=b) ? true : false;}function is_equal (a,b) { return (a==b) ? true : false;}a=1;b=2;alert("a=" + a);alert("b=" + b);if (is_less_than (a,b)) alert ("a is less than b");if (is_less_or_equal_than (a,b)) alert ("a is less or equal than b");if (is_more_than (a,b)) alert ("a is more than b");if (is_more_or_equal_than (a,b)) alert ("a is more or equal than b");if (is_equal (a,b)) alert ("a is equal to b");alert ("EOF:test.js.php<br>");';?>[/code]This works fine, however I need to change lots of code that escapes in to PHP and HTML lots and uses both ' and " so will make this task very hardI should imagine there is a function like setSimpleParser(), that stops the parser doing any html checks.Does any one have any clues on this one? Link to comment https://forums.phpfreaks.com/topic/30262-php-interepreter-error-with-external-jsphp-file/ Share on other sites More sharing options...
kenrbnsn Posted December 11, 2006 Share Posted December 11, 2006 Try outputing the code "<" instead of the "<" symbol. The browse may be seeing the "<" as the start of a tag and is ignoring it.Ken Link to comment https://forums.phpfreaks.com/topic/30262-php-interepreter-error-with-external-jsphp-file/#findComment-139198 Share on other sites More sharing options...
nevillejones Posted December 12, 2006 Author Share Posted December 12, 2006 [quote]Try outputing the code "<" instead of the "<" symbol.[/quote]Thank you for your comment! This is not really what I want to do as the example I have given I just created to test this problem. There are about 20 JavaScript files osCommerce with a mix of '<' and '<?php'.I cant change the '<?php' or php will not do it's job then, so I could create a regular expression that will change the correct '<'.Now thinking about it, changing '<' to '<' will stop the JavaScript from working however I could change '<' to '<?php echo "<"; ?>' which should work, I think.I will send an update once I know more. Link to comment https://forums.phpfreaks.com/topic/30262-php-interepreter-error-with-external-jsphp-file/#findComment-139637 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.