PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 29, 2022

[FIXED] How to defer inline jQuery in CakePHP 2.x

 January 29, 2022     cakephp, cakephp-2.x, jquery     No comments   

Issue

I load jQuery and other javascripts last in layout because of page speed optimization.

Now I have to make a jQuery script block in the view (mixing with php) but the jQuery functions obviously won't be recognised because it is loaded before jQuery is loaded.

I tried:

$this->Html->scriptStart(array('inline' => false));

but it doesn't even load it in the source code.

For debugging I tried:

$this->Html->scriptStart(array('inline' => true));

It does load but as it says, inline. No good for my needs.

How can I "defer" inline javascript and load it last, after all the other scripts and whole layout has been loaded?

One option would be to make it all with javascript but I'm going to use PHP there as well, that's why it's in the view.


Solution

Don't forget to include in your layout:

<? echo $this->fetch('script');?>

In your view, the following should now work:

<? $this->Html->scriptStart(array('inline' => false)) ?>
    //Js goes here
<? $this->Html->scriptEnd() ?>


Answered By - Inigo Flores
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing