Include all javascript files belonging to a folder in laravel 5.2 - javascript

Is there a way I could include all the javascript files belonging to a folder, from a .php file in laravel 5.2?
Something along the lines of:
<script type="text/javascript" src="{!! asset('js/*.js') !!}"></script>

You can use the following code to achieve your goal:
#foreach(Storage::files('/js') as $file)
<script type="text/javascript" src="{!! asset($file) !!}"></script>
#endforeach;
This is the first idea that comes to my mind.

$list = File::allFiles('/path/to/dir');
foreach ($list as $file)
{
echo '<script type="text/javascript" src="' . (string)$file . '"></script>
}
That'd include all files in a folder. That said, it'd probably be a better idea to create a file such as includes or bootstrap, where you list any and all files that you should include - and include THAT file instead. More maintainable that way, and if you wish for a file to just be temporarily "disabled", you wouldn't have to delete or move it.

Related

dynamic php header file with dynamic .js and .css include

I would like to do a dynamic include of my .css and .js file within the head tags in my header.php that depend on the current .php file that is open. So it will save some loading time if the .js or .css file is not needed for the current .php page.
So I thought I check the current url if it includes (strpos) a spicific string (e.g. "mycollection.php") and if yes echo the <link> and <script> tags to include the .css or .js files for the current, opened php file. But it seems to be hard to do a clean script this way. Does anyone has a clean, short and better solution for this problem?
Thank you in advance!
Try code below :
<!DOCTYPE html>
<html>
<head>
<?php
$get_url=$_SERVER['REQUEST_URI'];
$get_extension=explode('/',$get_url);
$get_extension_php=end($get_extension);
if($get_extension_php=='mycollection.php')
{
echo '<link href="your.css"/>';
echo '<script src="your.js"></script>';
}
?>
</head>
<body>
</body>
</html>
Hope it helps you
I used it before but not in the link but before the include of the header and it was like that
if (!isset($css){ echo "<link></link>;}
and at the header of the page if I didn't need to include the css file I just declare the variable $css = ''; and here we go hope this what you were asking for

PHP: How do I can access .js and .csss in Web dir from my Module in yii2?

I've an application build in Yii2 framework, and I've develop the application in backend side, but know I want to copy them become a module.
This my application dir
backend
--modules
--pstk
--controllers
--ValidationController.php
--DefaultController.php
--InterviewController.php
--StudentController.php
--SiteController.php
--UsersController.php
--models
--Validation.php
--Interview.php
--Student.php
--Users.php
--views
--validation
--_form.php
--view.php
--index.php
--update.php
--create.php
--interview
--student
--users
--Module.php
--web
--css
--manual_general.css
--js
--upload_header.js
--style
For example I've successfully run view.php from the module in browser, but the module can't access the .css and .js from backend/web/.
So the view displayed, but it's view mashed-up and all button didn't do any action when clicked.
Anyone know why the module didn't access the manual_general.css and upload_header.js from backend/web/? and how I can solve this problem?
Any help, will be appreciated. Thanks :)
EDITED:
This is the code in mi view.php to connect to .js and .css
<link rel="stylesheet" type="text/css" href="../../../../web/css/manual_general.css">
<script type="text/javascript" src="../../../../web/js/upload_header.js"></script>
Try using yii\helpers\Url::to, like:
<link rel="stylesheet" type="text/css" href="<?= Url::to('#web/css/manual_general.css') ?>">
<script type="text/javascript" src="<?= Url::to('#web/js/upload_header.js') ?>"></script>
Alternatively, in view.php you may do (assuming $this is your View instance):
<?php $this->registerCssFile('#web/css/manual_general.css'); ?>
<?php $this->registerJsFile('#web/js/upload_header.js'); ?>

Uncaught `TypeError: $(...).dataTable is not a function` in online server, but not in my local server

I have an error TypeError: $(...).dataTable is not a function when logged to my console after i uploaded my files to my online server, but in my local server, this won't appear and my app works fine.
I followed all the instructions here but still doesn't work. And I really don't know how to fix this.
Here's my script below:
<script src="<?php echo base_url();?>assets/grocery_crud/js/jquery-1.11.1.min.js"></script>
<script src="<?php echo base_url();?>assets/grocery_crud/js/jquery_plugins/config/jquery.noty.config.js"></script>
<script src="<?php echo base_url();?>assets/grocery_crud/js/common/lazyload-min.js"></script>
<script src="<?php echo base_url();?>assets/grocery_crud/js/common/list.js"></script>
<script src="<?php echo base_url();?>assets/js/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="<?php echo base_url();?>assets/js/plugins/datatables/dataTables.bootstrap.min.js"></script>
<script src="<?php echo base_url();?>assets/grocery_crud/themes/datatables/js/flexigrid.js"></script>
Please help me. Hope this won't be closed.
I found out that in my live server, the pathing of files is case-sensitive. Unlike in my local server, I can just mix cases and no issues found. In my case, when I call the script like this: <script src="mysite/assets/js/dataTables/jquery.dataTables.min.js"></script> , and the path is assets/js/datatables/jquery.dataTables.min.js , this won't work on a live server where datatables folder is not the same with dataTables folder in my path to call that file.
In the official documentation, it is telling to include datatables.min.js from the cdn. This script contains a definition of both jquery and DataTables.
The second script, bundles/datatables/js/datatables.js, is trying to patch jquery to add the functions initDataTables and dataTable.
Therefore, the order matters.
Here is my /symfony/templates/list.html.twig file for Symfony 4.2 :
<!-- Insert this at the end of your body element, but before the closing tag -->
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.2.1/dt-1.10.16/datatables.min.js"></script> {# Included libraries: jQuery 3 3.2.1, DataTables 1.10.16 #}
<script src="{{ asset('bundles/datatables/js/datatables.js') }}"></script> {# An omines script that patches jquery : add initDataTables function #}
<script>
$(function() {
// 'datatable' is defined in the symfony controller.
// datatable_settings Twig function renders a compact JSON string with the configured settings required for initialization.
var table = $('#presidents');
if (table && table.initDataTables)
table.initDataTables({{ datatable_settings(datatable) }});
});
</script>

Wordpress add javascript in footer

I need to add a script in the site footer.
I'm just referencing the footer.php:
<script src="wp-content/file/script.js"></ script>
At Home functions normally, but when access a page child he does not find because search the directory:
site/page-child/wp-content/file/script.js.
I saw that I have to use:
  wp_enqueue_script ()
But where should I put this code?
Thank's
You just need to add a "/" before your url in order to start it from root like :
<script src="/wp-content/file/script.js"></ script>
Indeed at home page it looks for yoursite.com/wp-content but on other pages it searches yoursite.com/current-page/wp-content and obviously it results in 404.
Adding / make it always look for yoursite.com/wp-content
Adding <script src="wp-content/file/script.js"></ script> is not a clean way to load your JS files because this is not a relative URL and when you active your child theme probably your theme will not load your JS file, the solution is :
<script src="<?php echo get_stylesheet_directory_uri(); ?>/js/script.js"></script>
You need to add this to your footer.php file in your theme directory wp-content\themes\your_theme\footer.php
but the best way is to use WP hooks to include your scripts, first you need to register them using
wp_register_script( 'my_script_name', get_template_directory_uri(). '/js/script.js' );
then simply load them
wp_enqueue_script( 'my_script_name' );
Don't try to register and enqueue your scripts directly in your footer.php file, instead create a method in your functions.php template file then load your scripts using
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
Note: To enqueue your scripts to the footer you need to set $in_footer parameter to true.
Checkout wp_enqueue_script for more information.
Since you seem to use WordPress, you can replace
<script src="wp-content/file/script.js"></ script>
with:
<script src="<?php site_url('/wp-content/file/script.js'); ?>"></script>

What's the correct way to write this jquery attr code? (wordpress)

I have this in my theme's function file:
$(document).ready(function() {
$(".ajax-loader").attr("src","<?php bloginfo('template_url'); ?>/images/ajax-loader.gif");
});
However, when it prints, the src prints as <?php bloginfo('template_url'); ?>/images/ajax-loader.gif, that is showing the php code instead of showing my template url. What would be the correct way to write this code?
Is this in a .js file? If so, you can't place WordPress template tags inside because .js files are not processed by PHP.
You can either include your code inline within header.php using <script> tags:
<script type="text/javascript">
$(document).ready(function() {
$(".ajax-loader").attr("src","<?php bloginfo('template_url'); ?>/images/ajax-loader.gif");
});
</script>
Or change the extension of your JavaScript file from .js to .php, and add this line at the very top:
<?php header('Content-Type: text/javascript'); ?>
That tells the server to treat this as a JavaScript file, although it'll be processed by PHP.
In any case, you're using .attr() correctly.

Categories