How to compress / reduce javascript with embedded php

In my jquery scripts there is php in them to trigger variables, etc. I am trying to use something like minify to squeeze it and hold back suspicious eyes. Php is causing problems. Has anyone done something like this?

here is just an example of my php-infused javascript:

$('input[name=type]:radio').filter('[value=<?=$type?>]').attr('checked', true); $('input[name=cert]:checkbox').filter('[value=<?=$cert?>]').attr('checked', true); $('input[name=gauge]:checkbox').filter('[value=<?=$gauge?>]').attr('checked', true); 
+4
source share
3 answers

PHP is trying to completely separate JavaScript in this way. In your PHP file, you save the values ​​as follows:

file.php:

 <html> <head>...</head> <body> ... <input type="hidden" id ="value_type" value="<?=$type?>" /> <input type="hidden" id ="value_cert" value="<?=$cert?>" /> <input type="hidden" id ="value_gauge" value="<?=$gauge?>" /> ... </body> </html> 

file.js:

 $(function() { $.data_values = { "type": $("#value_type").val(), "cert": $("#value_cert").val(), "gauge": $("#value_gauge").val() }; }); 

when you need to use values:

 $('input[name=type]:radio').filter('[value="'+$.data_values.type +'"]').attr('checked', true); $('input[name=cert]:checkbox').filter('[value="'+$.data_values.cert +'"]').attr('checked', true); $('input[name=gauge]:checkbox').filter('[value="'+$.data_values.gauge +'"]').attr('checked', true); 
+2
source

Minify will not work with PHP. If you need to keep PHP in it, there are not so many of them, you can replace it with a well-known tag (for example, "ABCDE", "12345"), then its thumbnail, then replace the tags with your PHP again.

+1
source

When all of your PHP is in JavaScript lines, as in your sample code, then any evaluation tool that deserves its salt should work fine. For example, if you want to use UglifyJS, you can try it on the Internet and see if it works for your code.

+1
source

Source: https://habr.com/ru/post/1342429/


All Articles