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

    • 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,
    • chamois_blanc
      By chamois_blanc+
      Hello,
      Shouldn't the image PL5 component be using the srcset/picture css directives to pick the correct size for an image? or it does already and I don't know how to use it? Thank you for any advice or enlightenment.
    • Dan Haddock
      By Dan Haddock
      Plugins Active and installed: 
      Akismet Anti-Spam
      Coming Soon Page & Maintenance Mode by SeedProd
      Contact Form 7
      Cookie Law Info
      DMS Professional Tools  
      Enhanced Media Library  
      Flamingo
      Google Analytics for WordPress by MonsterInsights
      Google XML Sitemap
      Jetpack by WordPress.com
      Legull 
      Loginizer   
      MailChimp for WordPress
      PageLines Updater    
      Schema App Structured Data 
      Sucuri Security - Auditing, Malware Scanner and Hardening
      Uber reCaptcha    
      Yoast SEO
      Wordpress Version: 4.8.1
      Pagelines Version: 2.2.4
       
      Good Evening/Afternoon or Morning, 
      I am running into a problem with CSS styling not applying inside of most browsers (Safari, Firefox) when I am logged in to wordpress, and all browsers that I have tested when I am logged off and the site has been made public (Chrome, Firefox and Safari).
      As a result, I have managed to identify the probable cause. It looks as though these browsers are refusing to render my sites compiled CSS file due to it being a HTTP URL rather than HTTPS.
      I have attempted to change this URL to HTTPS in my browser, however it brings back an ERROR: File not found page - https://www.shortfilmblog.com/wp-content/uploads/pagelines/compiled-css-core-1505847507.css
      What would you suggest in this regard? Would this be resolvable from your end?
      Happy to provide any further details required.
    • Borries
      By Borries+
      I know this is not an easy problem. Maybe there is a easy solution.
      I have tryed to uses the existing solution, that I have found in the forum
      I changed the colours of my nav (simple nav) and the hover effect (a. hover).
      I really tryed to highlight the active/used link in the nav but I cant figure it out.
      I want it to be black when active.
      Any help would be appreciated. Thanks.
      http://www.marcborries.de  nav link: "Sinn & Selbst" 
×