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

    • 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" 
    • Michael Kummer
      By Michael Kummer+
      Hi guys!
      I am using a plugin called Speed Booster Pack to speed up the load time of michaelkummer.com. One of the plugin settings, Minimize HTML and JavaScript causes an error that I only get with Pagelines. With other themes, such as WordPress' Twenty Seventeen, I do not get that error.
      I have reproduced the problem on my staging site and if you look at the JavaScript console, you will see the following error:
      SyntaxError: Unexpected end of script
      Any clue what PageLines may be doing differently to be contributing to this issue? I contacted the plugin developer and they, of course, pointed me to the theme developer
      Thanks
      Michael
    • flourishdesignstudio
      By flourishdesignstudio+
      Hello,
      We are working on a site and the customers are complaining that the site loads too slowly. They contacted their host who mentioned that the Java Script is all loading at the beginning. Is it possible to delay the load of the less important Java Script until after the initial load to speed up the site? The goal is to load the page faster by minimizing the initial javascript calls.
      Are there any preferred plugins that you would recommend or does this end up causing excessive problems with plugins, etc.?
      Thanks for the help!
×