Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-09-19 16:28:43

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Consolidation and compression of inline JS

I prefer to construct my pages with little self contained modules (forms containing txp/html and jquery). That way I can pick and choose which modules I want to use. Works for ease of editing and placement… But…

This results in js scattered all over my pages which is bad practice.

Anybody got a good idea or two for allowing me to keep js in my forms for editing, and presenting it all as one consolidated blob loaded right above the </body> tag?

I thought of (inside these modules) wrapping my inline scripts in form tags then loading all the js_prefixed forms inside some kind of compression tag (plugin needed) in one place right before the </body> tag.

That feels klunky and I’m sure someone else has figured out a better way…

speak!

Offline

#2 2013-09-19 18:05:59

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: Consolidation and compression of inline JS

I don’t know if this is a better way, but you can move <script> nodes to the end of <body> with etc_query:

<txp:etc_query data='<body>
...
<txp:output_form form="form_with_js" />
...
</body>'
query="."
replace="//script/=//body" />

Your js, however, shouldn’t contain </ string, this would bother the HTML4 PHP DOM parser.

Offline

#3 2013-09-19 18:44:52

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: Consolidation and compression of inline JS

Hi Oleg,

I often use txp tags in my scripts. I’m guessing that would preclude the use of etc_query… bummer.

-Dale

Offline

#4 2013-09-19 19:10:28

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: Consolidation and compression of inline JS

mrdale wrote:

I often use txp tags in my scripts. I’m guessing that would preclude the use of etc_query… bummer.

No, Dale (hi), not necessary, unless you do something complicated. Before etc_query gobbles it, data will already be parsed, so txp tags will be transformed in something else.

Offline

#5 2013-09-19 20:17:16

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: Consolidation and compression of inline JS

Silly idea: what if you include scripts in some <txp:variable /> inside your forms, and output it at the end:

<!-- form-->
...some stuff...
<txp:variable name="all_scripts">
<txp:variable name="all_scripts" />
<script>...</script>
</txp:variable>
...more stuff...
<!-- end form -->

<!-- page -->
<body>
...
<txp:variable name="all_scripts" />
</body>

Offline

#6 2013-09-19 20:47:08

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: Consolidation and compression of inline JS

That’s an excellent idea. each script adds itself to the variable. Now how about compressing white space? rah_replace/pax_grep? to stack overflow…

Offline

#7 2013-09-19 21:13:29

etc
Developer
Registered: 2010-11-11
Posts: 5,393
Website GitHub

Re: Consolidation and compression of inline JS

You don’t want to strip all white space, do you? I’d try

<txp:etc_query query='normalize-space(''<txp:variable name="all_scripts" />'')' />

Edit: though this is probably equivalent to preg_replace('/\s+/s', ' ', $thing), so you could use rah_function.

Last edited by etc (2013-09-19 21:19:32)

Offline

#8 2013-09-19 21:44:42

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: Consolidation and compression of inline JS

OK, this is my approach

Start with a form called “scripts_all” that contains…

<txp:variable name="scripts"><txp:variable name="scripts"/><txp:yeild /></txp:variable>

Then with each inline script use the following…

<txp:output_form form="scripts_all>(my script here)</txp:output_form>

Then right before the </body> call…

<txp:variable name="scripts" />

or even

<txp:mrd_strip><txp:variable name="scripts" /></txp:mrd_strip>

mrd_strip being a skinny function: preg_replace("/\s+/", " ", $thing);

Offline

#9 2013-09-19 21:53:58

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Consolidation and compression of inline JS

Blindly stripping whitespace may break the JavaScript depending on what the code contains. I always suggest that anyone touching JavaScript uses an actual tool that knows what its doing and runs the code through Lint before deploying to production.

We ourselves run our client-side code through Node.js stack (since it and its tools are appropriate for the job) using a tiny Grunt task configuration.

Last edited by Gocom (2013-09-19 21:55:25)

Offline

#10 2013-09-19 22:38:26

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: Consolidation and compression of inline JS

Yep… discovered that the hard way… but I do have it working…

// comments for one thing will bork things seriously

Can’t blame a guy for trying tho eh?

I am now able to load all my js in one place right above the /body tag (which I’m told is a good thing), while still being able to keep my modular forms.

Last edited by mrdale (2013-09-19 22:39:37)

Offline

Board footer

Powered by FluxBB