etriad
etriad

Reputation: 459

how to compress / minify javascript that has embedded php

My jquery scripts have some php in them to initiate variables, etc. I'm trying to use something like minify to compress it and deter prying eyes. The php is causing issues. Anybody done anything similar?

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);

Upvotes: 4

Views: 946

Answers (3)

andres descalzo
andres descalzo

Reputation: 14967

PHP attempts to completely separate the JavaScript in this way. In your PHP file you keep 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 have to use the 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);

Upvotes: 2

rsp
rsp

Reputation: 111316

When all of your PHP is in JavaScript strings like in your example code then any minification tool worth its salt should work just fine. If you want to use UglifyJS for example, then you can try it online and see if it works for your code.

Upvotes: 1

cusimar9
cusimar9

Reputation: 5259

Minify won't work with PHP. If you have to keep the PHP in there are there isn't too much of it, you could replace it with a known tag (i.e. 'ABCDE', '12345'), then minify it, then substitute the tags with your PHP again.

Upvotes: 1

Related Questions