I have a codeigniter project, where I want to use some js.
I have file with my js:
<script type='text/javascript' src='/utilities/assets/js/main.js'> and jquery
<script type='text/javascript' src='/utilities/assets/js/jquery.js'>
The both are availaible from my site, I can look it's content.
I have such code at main.js
$(document).ready(function ()
{
alert('Javascipt works');
}
When I load page, nothing happens.
I tried load javascript class of codeigniter, but when I tried in my view:
<?php echo $library_src;?>
<?php echo $script_head;?>
It says that that libraries are undefined. How can I use js there?
You are missing ); at the end. The function is never closed.
Try
$(document).ready(function ()
{
alert('Javascipt works');
});
instead of
$(document).ready(function ()
{
alert('Javascipt works');
}
Related
I wrote a code to add a js to specific pages in wordpress. but if I want to:
1- run the code in specific pages how can I change the code?
2- or run the code in everywhere except in some page IDs?
how can I cahnge that?
thanks in advance to everyone! :D
function wpb_hook_javascript() {
if (is_page ('727')) {
?>
<script type="text/javascript">
my script
</script>
<?php
}
}
add_action('wp_head', 'wpb_hook_javascript');
I think this would be as easy as
function wpb_hook_javascript() {
if (!is_page(array('the', 'excepted', 'pages'))) {
wp_enqueue_script('script_name', 'script_path.js')
}
}
add_action( 'wp_enqueue_scripts', 'wpb_hook_javascript' );
I used wp_enqueue_script because I like it more, but obviously you can stay with injecting it into the head directly, depending on what it does.
Edit: Pretty sure it would work like this:
add_action( 'wp_head', function() {
if (!is_page(array('the', 'excepted', 'pages'))) {
?>
<script type="text/javascript">
my script
</script>
<?php
}
}
Compare logical operators in php, is_page, arrays in php and wp_enqueue_script.
I currently call a file using onload in javascript.
It works fine, I just can't seem to get it to work in Laravel, if I load an html file it's fine, but not a php file.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("#query").load("test.php");
});
</script>
</head>
<div id="query"><img src='https://m.popkey.co/163fce/Llgbv_s-200x150.gif'/></div>
<div id="query2"><img src='https://m.popkey.co/163fce/Llgbv_s-200x150.gif'/></div>
Results are blank.
The test.php file is located in the public folder, it just has a simple echo in it:
<?php echo 'This is Working';
I have a test.html in the same folder that returns what ever I put in it perfectly, it seems to be an issue with javascript pulling the php file.
If i run this url : https://domain/Home/public/test.html it works fine, if i run this url https://Domain/Home/public/test.php it also works fine. The test.php echo's out This is Working
I had a similar issue a while back.
What i did was not run the file directly from the public folder, i created a Route.
Route::get('test.php', function(){
echo 'This is Working';
);
Now you can either run what you want directly from this function or return it to your view.
Now you can use :
<script type="text/javascript">
$(function() {
$("#query").load('test.php');
});
</script>
What Configuration are you using? This link shows that it's a problem using Laravel Valet. Github Issue Here.
I would recommend that you use an API route for loading these images and try to keep everything within the Framework for the sake of neat and tidiness?
But for a quick solution:
Route::get('foo.php', function () {
return 'foo';
});
API: (Within routes/api.php
Route::get('/test', function () {
return asset('assets/images/image.jpg');
});
and your JS would point to your URL of the Route.
Add the script tag of jQuery function in body and make sure you have used jQuery CDN for access the jQuery function
Can you please try this?
$("#query").load("{{ asset('public/test.php') }}");
I think this solve your problem
HI i am facing some problem while calling function from index.html on load event
it return error readjson is not defined in console.
index.html
<script type="text/javascript">
$(window).load(function () {
readjson('a','s');
});
</script>
main.js file
<script type="text/javascript">
function readjson(val1,val2){
//some code
}
</script>
Can anyone tell me why it is not able to call, i have link main.js file to index.html
JavaScript files shouldn't include any HTML.
Remove <script type="text/javascript"> and </script> from main.js.
You should see an error when that file is loaded (along the lines of Unexpected token <).
Call it like this inside the js file without the "script" tag :
function readjson(val1,val2){
//some code
}
In index.html you need the "script"
And follow the advice given in the comments, always include first the .js file and then the function in the index.html
Did you add jquery link properly??
If not, you should add jquery link for $(window) to be called. such as..
<script src="http://code.jquery.com/jquery-latest.js"></script>
If you actually have two script declarations as you are showing here the the window load will fire first. It then look for your function, which has not been created yet. That's why you are getting undefined.
You could have one script declaration on the page and place your function in the load function.
<script>
$(window).load(function () {
readjson('a','s');
function readjson(val1,val2){
//some code
}
});
</script>
Remove tags from main.js:
<script type="text/javascript"> and </script>
and remember to include jquery and main.js with:
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script>
<script src="test.js"></script>
So, I'm a novice at WordPress. I've got this in functions.php which loads the script on every page that gets the footer:
// flexslider custom settings
add_action('wp_footer', 'aplace_flexslider_settings');
function aplace_flexslider_settings() {
?>
<script>
jQuery(document).ready(function($){
$('.flexslider').flexslider();
});
</script>
Is there a way to customized this so that its called only on the static front page? For example, can I make a custom hook that I'd add_action to and then just get_customHook on the front page? That would disassociate it with the footer, right? I'm so confused...
Just wrap your function in an if statement:
function aplace_flexslider_settings() {
if(is_front_page()) { ?>
<script>
jQuery(document).ready(function($){
$('.flexslider').flexslider();
});
</script>
<?php } } ?>
Jquery:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/timeago.js" ></script>
$(document).ready(function(){
jQuery("abbr.timeago").timeago();
});
html:
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
function Refresh() {
setTimeout(function(){
<?php echo 'var id = '.json_encode($_GET['id']).';'; ?>
$('#cmdz').load('cmdajax.php?id='+id);
},1000);
}
The #cmdz div contains the abbr tag. timeago working properly in onload but when the div is refreshed it won't works.
for some reason jQuery("abbr.timeago").timeago(); function not working. Here you can find full code:after ajax call jquery function not working properly
Try out Livestamp.js. It's unobtrusive and auto-updating. All you need to provide is the Unix timestamp.
Working demo http://jsfiddle.net/FFeE3/1/
Can you please try sourcing your time ago.js from below link and any particular reason you are using jQuery("abbr.timeago").timeago(); you can use $("abbr.timeago").timeago();
Hope this helps rest demo should give you more idea. cheers
Script
<script type='text/javascript' src="https://github.com/petersendidit/jquery-timeago/raw/master/jquery.timeago.js"></script>
code
$("abbr.timeago").timeago();
Two way of representation, Do this way or other way but don't mix:-
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
OR
$(document).ready(function() {
$("abbr.timeago").timeago();
});