This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have three files a .php, a .html and a .js. How can I combine all three of them so that I can use the value of a variable set in php in JavaScript?
I will have to save this file by .php extenstion
<html>
<head> </head>
<body>
<?php
// here I can write my php code ?> // I can end it any time to include some JS code here
<script> Some js code</script>
<?php // and start again to write some more php code
?>
</body>
</html>
Related
This question already has answers here:
jquery .load with script in it
(1 answer)
how to jQuery .load() with script tags
(1 answer)
jQuery .load() call doesn't execute JavaScript in loaded HTML file
(13 answers)
Closed 2 years ago.
I'm trying to load html using jqyery. That HTML includes a js file, and every time I try to load it it doesn't execute js file included in it. But when I write the script inside the html page it works fine.
What doesn't work:
<link rel="stylesheet/less" href="/categories/categories.less">
<div id="categories">
</div>
<script type="module" src="/categories/categories.js"></script>
What does work:
<link rel="stylesheet/less" href="/categories/categories.less">
<div id="categories">
</div>
<script >
console.log('categories has been loaded');
</script>
This question already has answers here:
Script tags with src and code between script tags
(4 answers)
Closed 4 years ago.
I've looked around but couldn't find an answer to this.
How can I echo a script tag that has an src attribute?
When I try this:
echo '<script type="text/javascript" src="script.js">document.writeln(3);
</script>';
the code fails.
But when I do this and remove the src tag:
echo '<script type="text/javascript">document.writeln(3);</script>'
the code works.
what am I overlooking and how can I make the first version work?
You either include a src attribute, which references a file for the code or include the code directly in the script tag. You don't put both.
It should actually be like this:
echo '<script type="text/javascript" src="script.js"></script>';
echo '<script type="text/javascript">document.writeln(3);</script>';
This question already has answers here:
How do I redirect to another webpage?
(58 answers)
Closed 4 years ago.
I have a function that loads a page and add a new user.
I want to show an alert(); and redirect/load a different function to change the url.
I have used
<meta http-equiv="refresh" content="0;page.php"></meta>
<script>
alert("Pergunta eliminada com sucesso juntamente com as respostas!");
</script
Thank you!
use the window.location.href property to set the URL you want to redirect to:
window.location.href = "http://path.to.url/";
echo '<script type="text/javascript">
location.replace("profile/profile.php");
</script>';
you can use the above one to replace a file using js in php and can even add an alert here
This question already has answers here:
Wordpress how to use jquery and $ sign
(17 answers)
Why is my javascript file not found by wordpress? and why doesn't my javascript file refresh?
(6 answers)
Closed 5 years ago.
I have a page template which is applied to a specific page on my wordpress page. Usually for all of my other scripts i put them in my footer.php and they load from there beautifully. On this specific page though I want to load a script that only should be running on this page. So in my template i include it like this:
<?php
/**
* Template Name: Template
*/
?>
<?php get_header(); ?>
<script src="<?php echo get_template_directory_uri(); ?>/js/teacher-signup.js"></script>
<?php get_footer( ); ?>
Unfortunately when doing that I only get errors like:
Uncaught TypeError: $(...).whatever is not a function
Is there some way in Wordpress to include a couple of scripts on a specific template only? Or am I missing something?
Thanks in advance
register the script like wp recommends:
wp_register_script('your_file_name', get_template_directory_uri() . '/assets/js/some_file.js', false, null, true);
than enqueue the registered script for that specific page/template
if (is_page('page_name') ) {
wp_enqueue_script('your_file_name');
}
you can also add a conditional for the page template name if you want:
if (is_page_template("template_name.php')
You can make if conditional is_page() in certain page id / page slug for example
<?php if (is_page( 1 )) : ?>
<script src="<?php echo get_template_directory_uri(); ?>/js/teacher-signup.js"></script>
<?php endif; ?>
You can put it on your footer.php theme
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I am going to set a php variable with javascript, the php variable.
I want to give size of browser to a php variable and then echo best css for that (a kind of responsive website by different css and not css3).
<script src="jquery-1.10.2.js"></script>
<script>
$(function(){
var size=($(document).width());
if(size>300){
<?php
$size="more";
?>
}else if(size<300){
<?php
$size="less";
?>
}
})
</script>
<?php
if($size=="more"){
echo '<link rel="stylesheet" href="index-1024.css"/>';
}else if($size=="less"){
echo '<link rel="stylesheet" href="index-less-1024.css"/>';
}
?>
With this code php variable $size is always 'less'
PHP scripts are executed at server. and Javascripts(jQuery) scripts are executed at your browser. There is no way you can set variables like that. Perhaps consider switching the css entirely in javascript:
script src="jquery-1.10.2.js"></script>
<script>
$(function(){
var size=($(document).width());
//if large screen, swap in large css
if(size>300){
$('#mystyle').attr("href", "index-1024.css");
}
})
</script>
<?php
//load small css as default
echo '<link rel="stylesheet" href="index-less-1024.css" id="mystyle"/>';
?>