If your whole JavaScript code gets processed by PHP, then you can do it just like that.
If you have individual .js
files, and you don’t want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript.
For example, in your index.php
(or wherever you specify your layout), you’d do something like this:
<script type="text/javascript"> var my_var = <?php echo json_encode($my_var); ?>; </script>
You could then use my_var
in your JavaScript files.
This method also lets you pass other than just simple integer values, as json_encode()
also deals with arrays, strings, etc. correctly, serialising them into a format that JavaScript can use.
Related Posts:
- How to pop an alert message box using PHP?
- How to use php code in javascript
- How do I pass variables and data from PHP to JavaScript?
- Execute PHP function with onclick
- Mixed Content Page: requested an insecure stylesheet error
- SyntaxError: Unexpected token C in JSON at position 0 – Ionic 2 Http GET request
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- is there something like isset of php in javascript/jQuery?
- how to call a onclick function in tag?
- How do I refresh a DIV content?
- Only on Firefox “Loading failed for the script with source”
- How to open a URL using PHP?
- Keep getting a “Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup” when attempting to google plus login on my web app
- how to write javascript code inside php
- Ajax success function
- AngularJS POST Fails: Response for preflight has invalid HTTP status code 404
- WordPress Bootstrap Handburger Menu Wont Open
- AJAX not Working with php as supposed
- Getting a variable inside foreach from PHP to JS after localization
- Open image gallery on link click
- Failed to load resource: the server responded with a status of 404 (Not Found)
- What is NODE_ENV and how to use it in Express?
- How to deal with net::ERR_SSL_PROTOCOL_ERROR?
- What is the way of declaring an array in JavaScript?
- How to disable JavaScript in Chrome Developer Tools?
- Reactjs – Form input validation
- What is event bubbling and capturing?
- Angular: mat-form-field must contain a MatFormFieldControl
- JS – ReferenceError: fetch is not defined
- Check if a variable is a string in JavaScript
- How can I add a key/value pair to a JavaScript object?
- jQuery animate backgroundColor
- JavaScript get element by name
- How do I disable right click on my web page?
- Left() function in Javascript or jQuery
- Why is “forEach not a function” for this object?
- Completely disable scrolling of webpage
- Express error – TypeError: Router.use() requires middleware function but got a Object
- How to Open New window on every Click
- Jquery Ajax Call, doesn’t call Success or Error
- How to create a stopwatch using JavaScript?
- How to set onClick with JavaScript?
- How does += (plus equal) work?
- Javascript Comparison Operators != vs !==
- onEnter not called in React-Router
- Find the largest palindrome made from the product of two 3-digit numbers – Javascript
- Javascript/Jquery Convert string to array
- How to interpolate variables in strings in JavaScript, without concatenation?
- How can I use webpack with express?
- JavaScript check if variable exists (is defined/initialized)
- Embedding instagram webpage inside an iframe
- Do something if screen width is less than 960 px
- How to add a keyboard listener to my onClick handler?
- Javascript – Replace html using innerHTML
- Syntax error: Illegal return statement in JavaScript
- Error: TypeError: $(…).dialog is not a function
- Bootstrap modal – close modal when “call to action” button is clicked
- D3.js: what is ‘g’ in .append(“g”) D3.js code?
- Correct modification of state arrays in React.js
- Angular 2 Sort table columns
- JavaScript string encryption and decryption?
- How to dequeue a script?
- How to use wp_localize_script in custom page template?
- WP Customizer set a value via javascript (to create presets)
- preserve url parameters from affliliate sites
- How to overwrite JS variable with wp_localize _script?
- How to get value of selected page template in Gutenberg editor?
- How to speed up admin-ajax.php in wordpress
- JQuery not working on WordPress Admin page [closed]
- What javascript libs & frameworks are integrated in WordPress vanilla?
- Conditional fields in contact form 7 not working
- How to place script in footer?
- Enqueueing a script and a style sheet not working
- AngularJS with route and JSON API
- Uppload image from another source wp.media
- Status 400 for AJAX POST Request with admin-ajax.php
- Use onfocus event in Contact Form 7
- Get Block Attributes in JSON REST API
- Generate sidebar depending on height of post
- How to add specific meta tags to head of cart and checkout pages in woocommerce?
- enqueue script won’t work (enqueue style does work)
- Variable equals another variable and then equals another variable
- in jquery read a multidimensional array via wp_localize_script()
- get db values for external js file
- Ajaxurl suddenly not defined
- Social share buttons text shows up on post excerpts
- Plugin Admin panel checkbox unchecked after submit
- How to get file object from file_attachment?
- Creating a brand attribute as a variable
- Add other class content with reference class value
- How do I load urls from an external .js file to my admin head?
- Nothing in dashboard that uses javascript works
- Why does my custom plugin only function correctly once per page?
- Where is the Javascript attribute window._wpCustomHeaderSettings defined?
- How to execute WP shortcode in a javascript string “”
- JS files landing on page but not working, using childtheme of twentyeleven
- How do I convince this button to do something when it is clicked?
- Why this plugin is not working?
- Replacing & in JS code on pages
- Facing Problem While Running WordPress Hook For Archive, Categories, Author, Date Pages Only