0

Is there any way to minify/ compress an AJAX/Json script which removes the unnecessary whitespaces but leave the rest on is place, like

<?php } else { ?>

ecc. should stay there as it is.

The script is about 47 kb, too much for one page.

I've tried some online tools, but none of them are working, because they don't recognize the content of the code

Someone33
  • 568
  • 2
  • 8
  • 25
  • 1
    That's PHP, not JavaScript. Minifying JavaScript is easy (just google it) but probably not when mixed with PHP. – Denys Séguret May 14 '13 at 19:11
  • I know about compressing javascript, but i'm looking for a way to compress an AJAX/ JSON script, where php is inside it. – Someone33 May 14 '13 at 19:13
  • How can PHP be inside JavaScript ? Isn't it the opposite ? In any case, you should make your JavaScript static, not dynamic, if you want the browser to cache it, and to be able to minify it. That might mean separate the static from the dynamic in your script. – Denys Séguret May 14 '13 at 19:14
  • the whole site is an e-commerce framework, and so it's impossbile to separate php from js – Someone33 May 14 '13 at 19:17
  • Your "and so" is a little fast. It's one of the main goals in application architecture to separate modules. And it's generally done for e-commerce site as easily as with other dynamic javascript applications. – Denys Séguret May 14 '13 at 19:20
  • Sure it is, you're right. But my goal for this site is only to increase the performance. Cannot change the whole code for this site i'm working for. – Someone33 May 14 '13 at 19:30
  • So, if there isn't any other known way, i think i'll do it manually. – Someone33 May 14 '13 at 19:31

2 Answers2

0

Answering your question firstly you need to ask yourself why do you need it?

The purpose of compression data is to send/recieve it faster. It make sense for JavaScript, Images, CSS, HTML etc. and for that reason there are some tools.

For example: Best JavaScript compressor for JavaScript.

Apache has also a module for compression it is called Mod deflate You can use it for example via .htaccess file

To compress JS file

AddOutputFilterByType DEFLATE application/javascript 

To compress html

AddOutputFilterByType DEFLATE text/html

More you can read on http://httpd.apache.org/docs/2.2/mod/mod_deflate.html

Anyway, this is to reduce the time of page loading.

Php Scripts works on server side, the code is not shown in browser but the **result* of code is. That is why unless you care for the space on your server and you want to save 10 kB it has no matter! If you reduce php code it won't reduce the time of loading page. Instead you should make code better or use cache.

I hope you understand the difference between client side of scripts(JS) and server side(PHP).

you wrote in comment that your purpose is to " increase the performance" you will not achieve it by minifing code you will achieve it by making code better, profiling application, reduce unnecessary code, caching result of functions that take time and so on.

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
  • Solved my problem yesterday. Thanks for answering. I just don't know how to close this 3d. – Someone33 May 15 '13 at 22:12
  • select best answer and mark it as answer :) You can do it near two arrows and number there is mark sign use it ;) – Robert May 16 '13 at 08:52
-1

If it is not too much work for you then you can replace PHP code with some unique string, minify javascript and then replace string with original code.

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • I was thinking about that and maybe it's the only possibile solution. I was asking to avoid your hint. – Someone33 May 14 '13 at 19:20
  • I was trying to find minifier that skips code prefixed by some string but I found nothing. – Daniel Kmak May 14 '13 at 19:25
  • Me too. I'am searching for hours for this. There are a lot .js compressing tools online and similar stuff, but no one works in a porper way. So i think i'll follow your hint. – Someone33 May 14 '13 at 19:28