If you compress JavaScript file inside JS file, it is easy (.js). But if you try to compress JavaScript format which contains PHP, it is not pure JavaScript anymore. So, how to manage and get JS file into one row? can be used a tool for this option?
If you compress JavaScript file inside JS file, it is easy (.js). But if you try to compress JavaScript format which contains PHP, it is not pure JavaScript anymore. So, how to manage and get JS file into one row? Can be used a tool for this option which detects PHP injects?
I have tested and it is like I thought. If there is pure JavaScript programming code it will be compressed. An example: https://jscompress.com/
But if you put programming code compressor tools can not decide what are injects. Can be solved this option using PHP code for the compressed code?
An example for injects error: Unexpected token: operator «<» (line: 102, col: 52)
The solution to your issue here is to stop trying to mix your PHP and JavaScript code. Write pure JavaScript code, and access whatever data you need from PHP another way (fetch, data attributes, hidden inputs, etc).
Well if you compress a template literal, you’ll erase the line breaks. Which should again be fine because HTML doesnt care about whitespaces either.
Dunno what the upside down p is doing there; and I cannot debug a code compressor tool i dont create or manage.
I get the impression this is you wanting someone to wave a magic wand and say “push this button and it will all work” when its just not that simple. As has been pointed out at least twice now, your problem may be more fundamentally what you’re putting where and how you’re going about accessing it, rather than whether or not it’s got a couple of extra \r\n’s in it.
Agreed. Mixing your JS and PHP has the potential to be a nightmare for debugging and testing your code. In my opinion a horrible idea.
This is only rushed as I am off out the door, but why not fetch your data from PHP in JSON format. Something like this.
// Template for your HTML
const createUserHTML = function (props) {
const { name, email, website } = props;
return `
<div>
<h2 class="name">${name}</h2>
<p class="email"><b>Email :</b> ${email}</p>
<p class="website"><b>Website :</b> ${website}</p>
</div>
`;
}
// fetch JSON data from your PHP
const fetchUser = async function (id = 1) {
const response = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
return response.json();
}
const renderUser = async function(id) {
const userContainer = document.querySelector('#user');
// fetch the PHP data
const userData = await fetchUser(id);
// create the HTML
const html = createUserHTML(userData);
// could the HTML be compressed at this point if really required?
// render to page
userContainer.insertAdjacentHTML('afterbegin', html);
}
renderUser(2)