My header is calling a javascript file which sends out an email:
<script type="text/javascript" src="<?php bloginfo('template_directory') ?>/css/effects.js"></script>
But inside this file, I have a jQuery code that calls a .php file that does the actual sending of the email:
$.ajax({
type: "POST",
url: "css/sendmail.php",
data: dataString`
But the script doesn't work, unless the url is:
<?php bloginfo('template_directory') ?>/css/sendmail.php
and not just:
css/sendmail.php
Is there any way to include a path to the wordpress template directory inside js?
You could create a Javascript snippet that saves the template dir in a variable, and use this later:
<script>
var templateDir = "<?php bloginfo('template_directory') ?>";
</script>
Related
I have a separate js file into laravel public folder, I have decleard my route into web.php, I can use them in blade file but getting error while try to use them in that js file.
$.ajax({
//url:"http://127.0.0.1:8000/cats/fetch",
url:"{{route('cats.fetch')}}",
method:"POST",
data:{select:select, value:value, _token:_token, dependent:dependent},
success:function(result)
{
$('#'+dependent).html(result);
}
})
but when I use hard coded url then its work for instance url:"cats/fetch".
How can I make it configurable not hard coded into js file
url:"{{route('cats.fetch')}}", is not valid .js syntax. You're correct that you can use it in .blade.php, but in an external .js file, you need to assign it to a variable first:
<script type="text/javascript">
let url = "{{ route('cats.fetch') }}";
</script>
<script src="path/to/file.js" type="text/javascript"/>
Then, in file.js, reference the variable:
...
url: url,
...
I have .mht files that are stored on a folder on my server. This folder has a rule in .htaccess that only localhost is allowed inside.
I need to render files on my website depending on user actions.
<iframe> and <embed> and jquery .load() won't work as the request doesn't come from localhost.
I'm trying to get the data of the file via a phpscript and an ajax call :
PHP:
$file = htmlentities(filter_var($_GET['url'], FILTER_SANITIZE_STRING), ENT_QUOTES);
$content = file_get_contents(".".$file);
$return = array("content" => $content);
echo json_encode($return);
jQuery:
$.getJSON('queries.php',{q: 'getFile', url: file},
function(data){
$('#file_panel').html(data.content);
}
);
But it only displays the content of the file, it doesn't render it. Any suggestion to get it rendered?
It will not be possible unless you use an <iframe>. Browsers won't parse HTML & MHT within the same page.
Try this,
PHP File
$file = htmlentities(filter_var($_GET['url'], FILTER_SANITIZE_STRING), ENT_QUOTES);
echo file_get_contents(".".$file);
JS
$("#frame").attr("src", "queries.php?url="+file);
I included Var with url string to JS files directory in my PHP page:
var jsDir= "<?php echo get_template_directory_uri(); ?>/js";
Now I need to add each JS files within .js file using jQuery.getScript and I need help adding the directory var jsDir to the url string:
$.getScript( "/plugin.js");
This is pretty trivial string concatenation
$.getScript( jsDir + "/plugin.js");
I want to retrieve source code of a javascript file( eg: xxx.js) from some url. I could get html from url but when I try to do get .js file using same method, it fails.
How can I get a whole source of js file from url?
Here's what worked for HTML :
$.ajax({
url : "http://localhost:8080/aaa/bbb/xxx.html",
success : function(result){
var a= result;
alert(a);
}
});
Use this
<script type="text/javascript" src="xxx.js"></script>
Trying to implement this jquery plugin http://www.myjqueryplugins.com/jquery-plugin/jrating i have trouble setting paths for stars in js file. its not possible to use php inside js file right?
Cause I need to set those stars path to be inside webroot folder and i don't know how to do that without WWW_ROOT constant
(function($) {
$.fn.jRating = function(op) {
var defaults = {
bigStarsPath : 'icons/stars.png'
...
Just make it an absolute URL instead of a relative URL:
(function($) {
$.fn.jRating = function(op) {
var defaults = {
bigStarsPath : '/icons/stars.png' //<-- notice the "/" before "icons"
...
That tells it to look for an 'icons' folder within the 'webroot' folder.
The same goes for any other files in the webroot. Sometimes you want to include css or javascript file that's within a library or something - in that case, you can include it by setting a "/" first - like this: /bootstrap/js/main.js.
There are many solutions:
Copy this code to view file (.ctp) and use:
bigStarsPath: '<?php echo $path_to_file; ?>'
If You can add .js files to preprocessor of PHP:
Parse a JavaScript file through PHP
Set in view js variable and read in js.
in your header file put this code below script with set constant variable and use that JavaScript variable name called bipartisanship in your JS file, JS must be include after this code.
<?php define('bipartisanship','/icons'); ?>
<script type="text/javascript">
var bipartisanship = '<?php echo bipartisanship; ?>'
</script>