Jump to content

Archived

This topic is now archived and is closed to further replies.

wesleymusgrove

Execute Simple JavaScript in LESS

Recommended Posts

wesleymusgrove

I'm trying to execute some simple JavaScript inline in the style.less file of my child theme.  But it causes some LESS compiler error and completely breaks the DMS editor so I can't edit anything or use the toolbar on the frontend.

@all-background-colors: "#D09EC8,#F2DD8B,#84e0cd,#F3904E";
@background-color: color(~`@{all-background-colors}.split(',')[Math.floor(Math.random()*@{all-background-colors}.split(',').length)]`);

body {
  background: @background-color;
}

On http://less2css.org/, this snippet works and randomly changes the body's background to 1 of the 4 colors in @all-background-colors.

Even something as simple as this anonymous function breaks the compiler:

@color: ~`(function(){ return "red"; })()`;

body {
    background: @color;
}

The only way to fix the DMS editor is to remove the JS from my style.less file, and then edit and save the LESS fallback section in the dashboard.

I tried setting PL_LESS_DEV in my wp-config.php according to http://www.pagelinestheme.com/dms-custom-less-tips/, but it doesn't clear the cache every page refresh like it's supposed to.

define('PL_LESS_DEV',true);

Is it possible to execute this kind of JavaScript in the style.less file of a DMS child theme?

Share this post


Link to post
Share on other sites
Simon

The less compiler in dms is php not JavaScript I'm afraid this will not work

Share this post


Link to post
Share on other sites
wesleymusgrove

The less compiler in dms is php not JavaScript I'm afraid this will not work

Is there any way to include less.js and a custom.less file that contains this type of JavaScript earlier in the <head>, for client-side compilation?

..So that any less variable created by executing js (in custom.less) would be available to the php compiled less (style.css) later on in the <head>?

Share this post


Link to post
Share on other sites
Simon

If all you want is random colours this would be easy with just jquery 

Share this post


Link to post
Share on other sites
wesleymusgrove

I know it could be easily done with jquery.

My previous question was about using less.js to compile a custom.less file on the client side.  Is this possible to add to the <head> before the style.css is included?

I'm talking about the Client Side Usage example on http://lesscss.org.

Also why does PL_LESS_DEV config option not clear the less/css cache as advertised?

Share this post


Link to post
Share on other sites
Simon

less.js isnt used, the php compiler creates a css file based on whats in the less files. If you want to include a less.js file and a custom less file for it to parse then go for it add it to the head of the page using standard wordpress enqueue functions. 

Seems a bit overkill to add a huge less.js file and an extra less file just to change some colours randomly when you could add 3 lines of js and do the same thing.

Share this post


Link to post
Share on other sites
wesleymusgrove

Right thanks.  I'm not only wanting to do something as trivial as changing colors.  I just used that as a simple example and proof-of-concept for the sake of this forum post.

Why does PL_LESS_DEV config option not clear the less/css cache as advertised?  I have it in my wp-config.php, but I still have to go in to the DMS editor Resets section and clear LESS/CSS cache every time I make a change in style.less.

Share this post


Link to post
Share on other sites
Simon

Right thanks.  I'm not only wanting to do something as trivial as changing colors.  I just used that as a simple example and proof-of-concept for the sake of this forum post.

Why does PL_LESS_DEV config option not clear the less/css cache as advertised?  I have it in my wp-config.php, but I still have to go in to the DMS editor Resets section and clear LESS/CSS cache every time I make a change in style.less.

​Do you have access to the dev plugin? It has a dev mode checkbox

Share this post


Link to post
Share on other sites
wesleymusgrove

I do. Forgot about the dev plugin. Thanks.

Share this post


Link to post
Share on other sites

  • Similar Content

    • Michael Kummer
      By Michael Kummer+
      Hi guys!
      I have noticed that my primary sidebar becomes intermittently invisible when browsing through various blog articles on michaelkummer.com/blog.
      See:
      https://www.dropbox.com/s/z646sgoa3mfjl1j/Screenshot 2018-07-12 15.57.46.jpg?dl=0 and https://www.dropbox.com/s/vdledzzgginvwgv/Screenshot 2018-07-12 15.57.57.jpg?dl=0
      Upon refresh, the sidebar appears. I have been using the following CSS code to hide the sidebar on mobile without any issues. 
      @media only screen and (max-width: 993px) {
      /* hide sidebars */
           .plsb {
              display: none !important;
          }
      }
      I tried to add the following CSS to force the sidebar to show on screens that are wider than 994 pixels but to no avail. Any clue what could be causing this issue?
    • Michael Kummer
      By Michael Kummer+
      Hi guys!
      I am using the LESS/CSS Pagelines extension and tried to add the following code to make use of FontAwesome:
      a.c2a:before {
          content: "\f07a\00a0";
          font-family: "Font Awesome 5 Pro";
          Font-weight: 900;
          font-style: normal;
          font-weight: normal;
          text-decoration: inherit;
          padding-left: 4px;
      }
       
      The issue is that as soon as I have the CSS via the Custom Styling text box, Pagelines (or the extension) strips out the backslashes (\). As a result, I had to abandon the CSS/LESS extension and instead add all my CSS code to a separate .css file.
      Is there a fix or workaround for this?
      Thanks
      Michael
    • glyph.marketing
      By glyph.marketing+
      Hello, I am having a problem similar to that experienced by Michael Kummer here. We have pages that show this automatic scrolling issue (examples here and here) and it is sufficiently annoying that we have seen customers bounce while trying to convert. Strangely, it only happens at specific resolutions, so it can be difficult to reproduce. In general we can only consistently reproduce it on a 4k resolution with a browser width between 1517 and 1536px. 
      Thanks to Michael Kummer's help we were able to set the overflow-anchor property to none in the entire body, which fixed the issue as far as we can tell, but also obviously disabled scroll anchoring for the whole site. I tried applying it to the specific element that looked like it was being reloaded, which was the PL Meganav extension, but that was unsuccessful. 
      Can anyone help us figure out which element or ID we can apply this property to, so that we can fix the scrolling issue but not disable scroll anchoring for the entire site?
      Thank you - 
      Glyph Language Services
    • IDdigital
      By IDdigital+
      Hi,
      A site of ours irregularly does not finish loading. The page in question is https://rw360.org/grievance-process/
      In Safari, I'll occasionally get the following error:
      [Error] TypeError: undefined is not an object (evaluating 'a.plModel.init')
          (anonymous function) (site.js:5:91)
          i (jquery.js:2:27455)
          add (jquery.js:2:27750)
          ready (jquery.js:2:29819)
          (anonymous function) (site.js:5)
          Global Code (site.js:5:3421)
      This seems to be a bug in PageLines as far as I can tell. Any suggestions or a fix coming?
      Thanks,
×