Can you put php inside javascript
EDIT : Was speaking to a friend of mine about this who often puts ruby in his JavaScript and he brought up this point. Is it ok to dynamically place application wide constants in your JavaScript so you don't have to edit two files. Try decoupling the two languages by making data their only interface -- don't mingle the code. In your example, you could improve the code by using PHP to populate a cfg structure that's available to JavaScript:. This way, PHP only cares about populating the data structure and JavaScript only cares about consuming the data structure.
To answer the additional questions you asked with your update, yes, it would be good practice to apply the DRY principle and let PHP and JavaScript share the same configuration object:.
There is no harm in inserting the JSON representation of your configuration directly into your page like this. You don't necessarily have to fetch it via XHR. What you're supposed to do is comprehend what Seperation of Concerns and Progressive Enhancement mean.
The biggest problem with your snippet is you're missing the to make it a valid jQuery selector ;. What's wrong with changing the selector in your click handler to a class, and adding the class to the element in question if you want the handler to be fired, and not if you don't;. There are circumstances where you need to include PHP in your JavaScript; but I must admit these are few and far between.
Once example is when you have different environments; test, staging and live. Each of them have a different location of your assets images, mainly. The easiest way to set the path so that it can be used by JavaScript is something like;.
This is a bad practice in my opinion, as you would need to call this file something. And then you could call this function in a php file, to make these mixing things as small as possible. By doing this having bigger JS files, not lines where it doesn't matter you can take advantage of compressing the JS file and front-end developers feel much comfortable working only with JavaScript in my opinion as you could write Python, Ruby etc not only PHP - and the code could get bigger and bigger depending on what you need to do there.
I would consider this bad practice. When putting dynamic content inside script blocks you always must be aware of the fact that escaping inside a javascript context is not as simple as you would hope. If values were user-provided, it is not sufficient to html-escape them.
I don't think this is bad practice. If the ID required in your JavaScript is dynamic there is no other way to do this. Some people would argue that it's bad practice. I would say that in general don't do it. Here the code is just passing data from php to javascript, not creating logic on the fly or the like. The good part of doing this vs an ajax call is that the data is available as soon as the page loads and does not require an extra trip to the server. Of course an other option is to build a javascript config file from PHP via some form of build script that will put that into a.
Also because its it in script it therefore doesn't show and it can sometimes take a while to realize why your script is broken. It depends by whom, and if you ask me, yes I do consider it back practice for a few reasons. First of all, I'd prefer to have javascript code in its own JS file that the php parser would not be able to touch. Secondly, php executes at server time only, so if you are depending on some variable in php to change your javascript, that may not work very well.
If there is some page-load setting you want to control with javascript, I typically prefer to add that value to the DOM with php so that javascript can reach it when and if it wants to in a hidden div, for example. Finally, just for organizational purposes, this can get very annoying.
It's bad enough to mix html and php in my opinion. Yeah, exactly. You got it! Just give it an id so that you can use document.
It's all just javascript : — Jeremy Jackson. Thanks for helping!! I tried, but still getting the NaN. I tried putting that parseInt up there too, but no luck I got it!!
Show 2 more comments. Pedro Lobito Pedro Lobito Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Send Update Cancel. Email ID. Attach files. Desktop Google Docs. Each Attachment size should not exceed 1. Max no of attachments : 3. Loading User Profile Response title.
Publish Back to edit Cancel. Sign In. New to this Portal? Click here to Sign up. You can also use the below options to login. Search jQuery Search. Move this topic Cancel. Getting Started. How to use php function inside javascript file? Hello i have this function for languages using php. Replies Re: How to use php function inside javascript file? In the second it runs while the page is being rendered. Leave a comment on jakecigar's reply.
You can't. PHP runs on the server. It reads your.
0コメント