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");
Related
I have a javascript code that extracts filename as a variable from the current html file. The filename, for example, is "new.html" filename variable is successfully used to append href where I need to open same file strored in another folder. Using the same code, I need to append this variable to a folder path with href tag to download a file with href tag. The file name is extracted from .html (example new) and added to .xls file (example new.xls)
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
console.log(filename);
document.getElementById("htag1").href= "Foldername/"+filename;
var object=filename.slice(0,-5);
var xls=".xls";
var xlsfile=object+xls;
$(".xlsfile").text(xlsfile);
console.log(xlsfile);
document.getElementById("d1tag1").href= "Foldername/"+xlsfile;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#working
#not working
####working download code####
But this is giving me download error with no file getting downloaded
but it is pointing to the right path. Should I use another way for download feature? Before this I had entered the file path statically which seemed to work.
Any help would be appreciated! Thank you!
Try this way, i'm sure now it will work ^^
var filename=location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
console.log(filename);
document.getElementById("htag1").href= "Foldername/"+filename;
var object=filename.split(".")[0];
var xls=".xls";
var xlsfile=object+xls;
$(".xlsfile").text(xlsfile);
console.log(xlsfile);
document.getElementById("d1tag1").href= "Foldername/"+xlsfile;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
#working
#not working
####working download code####
I'm trying to access image which is located in ~\Content\img folder. I'm trying to do that from JavaScript file which is located in ~\Scripts folder. This an MVC application.
U have tried absUrl + "\Content\img" + fileName. But it gives me Controller\Content\img\fileName.jpg
Forward slashes...
\Content\img\fileName.jpg
Should be
/Content/img/fileName.jpg
Please use the #Url.Content helper:
#Url.Content("~/img/fileName.png")
In case you use a separate javascript file you can put a <script> block in the beginning of the view page:
<script>
var ROOT = '#Url.Content("~/")';
</script>
And then refer to the ROOT variable in javascript:
var imagePath = ROOT + '/img/fileName.png';
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>
i want to print all text from .js file to div
document.getElementById('cart-status').innerHTML = "/numberOfcart.js"
You could use jQuery to do something like this:
$.get('/numberOfcart.js', function(data) {
$('#cart-status').text(data);
}
but you are simply loading in the .js file, not running it.
Directly displaying the the contents of js file by JavaScript is not possible (or I don`t know, if somebody knows a way - welcome). So by PHP:
$file = file_get_contents( 'script.js' );
echo "<pre>" . htmlentities ( $file ) . "</pre>";
With an ajax callback to the php script, set to the div.
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>