Is it possible to use coffeescript inside .php file, for example :
<!--test.php-->
<html>
<head>
<title> use coffeescript inside php file
</head>
<body>
<h1>how to use coffeescript</h1>
<p><?php echo $lorem ?></p>
<script type="text/javascript">
// i want to use coffeescript here
(function() {
alert ('hello world')
});
</script>
</body>
</html>
If I want to use coffeescript in my php file what should I do to make it work?
I see no reason why not.
https://github.com/alxlit/coffeescript-php
Load your coffee in a var, compile it, and stick the resulting JS variable between the <script> tags.
Related
I have three php files, one defines a constant, the other has the html markup and includes a php file wigh renders js content. the third is the php file rendering the js content. the code is as shown below.
the first file named "config.php" :
<?php
define('TESTVAR','test variable');
?>
the second file named "main.php" :
<?php
require_once ('config.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script type="text/javascript" src="script.php"></script>
</body>
</html>
the third file named "script.php" :
alert(<?php echo TESTVAR ?>);
this is what gets loaded as script.php:
<b>Notice</b>: Use of undefined constant TESTVAR - assumed 'TESTVAR' in <b>C:\xampp\htdocs\trials\testphpinjs\script.php</b> on line <b>2</b><br />
TESTVAR);
it's not that php is not being rendered, if i change the script.php to
alert(<?php echo '\'testing'. 'php' . 'echo\'' ?>);
it works perfectly and the loaded script can be viewed from developer tools as
alert('testingphpecho');
the only problem I have is that I cannot access the variables in pages required or included before
Using <script type="text/javascript" src="script.php"></script>, the browser will issue a new request to load script.php directly. Since PHP is stateless, that request will not be handled in the same context as whatever processing goes on in main.php.
If you want to use the variables/constants defined in config.php within script.php, you will have to load it in there:
<?php require_once('config.php'); ?>
alert("<?php echo TESTVAR; ?>");
Note that I've added quotation marks, since TEST_VAR is a string value in your example.
If your Javascript code relies on more than just config.php and its behavior depends on pre-processing that's done in main.php, it might be better to load it as an inline script instead:
<?php
require_once ('config.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
<?php include('script.php'); ?>
</script>
</body>
</html>
This way, PHP is loading the script.php file within the same context as main.php, and all constants and variables defined there will (should) be available for use by the code that's outputting your Javascript.
I have a Wordpress plugin. inside this plugin there is a php file.(for example name.php) this php file contains some codes which they are inside this code:
<script type="text/html" id="tmpl-dokan-single-variations">
//codes
</script>
Now I want to add some Javascript codes and I've tried this code:
<script type="text/html" id="tmpl-dokan-single-variations">
<script type="text/javascript">
//mycodes
</script>
//codes
</script>
Also I've tried echo script but it doesn't work either.
Please tell me how can I add some javascript code inside this file?
There is no way to include the raw string </script> inside a script element.
You would need to find some alternative representation of it (such as <!-- TEMPLATE: SCRIPT END TAG --> and then modify the code (which is presumably JavaScript) that reads the value of the <script type="text/html"> element so it can replace that alternative representation with the end tag you actually want.
I dont know what your php file looks like but you can echo your script like this
<?php
echo '
<script>
// your code here
</script>
';
?>
I have a requirement to access apache environment variable from js file. I am not sure this is possible or not.
I used to set Apache variables and access it in php using the following way
Set ENV varialbe
SetEnv PAYPAL_MODE live
From php
echo $_SERVER["PAYPAL_MODE"];
My question is can i access "PAYPAL_MODE" in my js file
<html>
<head>
alert(PAYPAL_MODE);
</head>
<body>
</body>
</html>
You can do this:
var paypal_mode = "<?= $_SERVER["PAYPAL_MODE"];?>";
alert(paypal_mode);
You need to be sure that you want expose that variable
Try this,
<head>
<script>
alert("<?= $_SERVER["PAYPAL_MODE"];?>");
</script>
</head>
Well, you could do something like this in your html :
<script>
$(document).ready(function() {
window.paypal_mode = <?php echo json_encode($_SERVER["PAYPAL_MODE"]); ?>;
});
</script>
And access it through the window object in your javascript file.
I am trying to load php code into div tag to show the results but the below code doesn't work for.
Can anyone please help me...to resolve this
my code is below:
<html>
<head>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$("document").ready(function()
{
$("#myDiv").load("refreshing.php");
}
</script>
</head>
<body>
<div id='myDiv'> </div>
</body>
</html>
Change script tag to this
<script type="text/javascript">
$(document).ready(function()
{
$("#myDiv").load("refreshing.php");
});
</script>
i.e. you're missing ); at the end
Also, document should not have quotes on it
You cannot load php code .load() because as the web server gets the GET request with .php, the request is passed to PHP interpreter which will execute the PHP script and return the result of the php script instead of code. So what you can do is in your PHP script, do a echo of all the code that you want to display. Or, you can define custom rule in your Apache configuration.
You've forgotten the closing bracket:
$("document").ready(function(){
$("#myDiv").load("refreshing.php");
});
I have a .json file in a bucket on S3. I'm trying to parse information from the file, a date and a SigninSim. I am doing this through an html file which once I get this figured out will take that parsed information, go into another folder, and display some pictures. Here is the code that I currently have written.
<!DOCTYPE html>
<html>
<head>
<script src="https://s3.amazonaws.com/'BUCKET'/browser.json"></script>
<script type="text/javascript">
function parseJSON()
{
var info = JSON.parse(browser);
document.write(info.date);
document.write(info.SigninSim);
}
</script>
</head>
<body>
<script type="text/javascript">
parseJSON();
</script>
</body>
</html>
When I run this nothing shows up on the page. Any ideas? I'm also very new to html/javascript so I could be doing something completely wrong, anything helps!
<script> tags can only be used to execute Javascript code, not to read JSON files.
Change the JSON file to a Javascript file that creates global variables or objects.