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

    • gstrahle
      By gstrahle+
      Hi,
      I am hoping to figure out how I can get my PageLines Full Nav to display layered in front of a sticky footer element on my website - 3keymedia.com
      I would like the Full Nav to overlay the entire screen when opening the menu as you will see when opening on a desktop the footer of nav content is cropped by the sticky footer.
       
      Thanks


    • gstrahle
      By gstrahle+
      Hi,
      Is there anything that can possible be done to improve the loading performance of PageLines Platform and Elements?
      https://goldenstategreens.com/wp-content/plugins/pl-platform/engine/ui/js/common.js?ver=946207(180.5KiB) https://maps.gstatic.com/maps-api-v3/embed/js/36/11/init_embed.js(146.6KiB) https://maps.googleapis.com/maps/api/js?client=google-maps-embed&paint_origin=&libraries=geometry,search&v=3.exp&language=en_US®ion=us(99.3KiB) https://goldenstategreens.com/wp-includes/js/jquery/jquery.js?ver=1.12.4(87.0KiB) https://goldenstategreens.com/wp-content/plugins/pl-section-slider/flickity.js?ver=946207(48.2KiB) https://goldenstategreens.com/wp-content/plugins/pl-section-impulse/owl.js?ver=946207(35.3KiB) https://goldenstategreens.com/wp-content/plugins/pl-platform/engine/ui/js/site.js?ver=946207(18.8KiB) https://goldenstategreens.com/(17.5KiB of inline JavaScript) https://secure.gravatar.com/js/gprofiles.js?ver=2019Apraa(15.9KiB) https://goldenstategreens.com/wp-includes/js/jquery/ui/effect.min.js?ver=1.11.4(11.6KiB) https://goldenstategreens.com/wp-includes/js/jquery/ui/tabs.min.js?ver=1.11.4(9.6KiB) https://s0.wp.com/wp-content/js/devicepx-jetpack.js?ver=201917(9.4KiB) https://goldenstategreens.com/wp-content/plugins/pl-section-lax/parallax.min.js?ver=946207(7.8KiB) https://goldenstategreens.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1(7.7KiB) https://goldenstategreens.com/wp-includes/js/jquery/ui/accordion.min.js?ver=1.11.4(6.4KiB) https://goldenstategreens.com/wp-includes/js/jquery/ui/widget.min.js?ver=1.11.4(6.1KiB) https://goldenstategreens.com/wp-content/plugins/pl-section-meganav/superfish.js?ver=946207(4.4KiB) https://goldenstategreens.com/wp-includes/js/jquery/ui/core.min.js?ver=1.11.4(3.3KiB) https://goldenstategreens.com/wp-content/plugins/pl-section-meganav/meganav.js?ver=946207(2.2KiB) https://goldenstategreens.com/wp-content/plugins/pl-section-meganav/sticky.js?ver=946207(2.2KiB) https://goldenstategreens.com/wp-content/plugins/pl-section-impulse/impulse.js?ver=946207(1.7KiB) https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3355.356766011451!2d-117.21045368481853!3d32.75626678097826!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x80deaaeffa185a7f:0x6613dd77eb86c21!2sGolden+State+Greens!5e0!3m2!1sen!2sus!4v1552542527201(1.5KiB of inline JavaScript) https://goldenstategreens.com/wp-includes/js/wp-embed.min.js?ver=5.1.1(1.2KiB) https://goldenstategreens.com/wp-content/plugins/google-analyticator/external-tracking.min.js?ver=6.5.4(975B) https://goldenstategreens.com/wp-content/plugins/jetpack/modules/wpgroho.js?ver=5.1.1(716B) https://goldenstategreens.com/wp-content/plugins/jetpack/_inc/build/widgets/milestone/milestone.min.js?ver=20160520(488B) https://goldenstategreens.com/wp-content/plugins/pl-section-flicky-posts/js/script.js?ver=946207(469B) https://goldenstategreens.com/wp-content/plugins/jetpack/_inc/build/photon/photon.min.js?ver=20130122(395B) https://goldenstategreens.com/wp-content/plugins/pl-section-lax/lax.js?ver=946207(352B) https://goldenstategreens.com/wp-content/plugins/pl-section-slider/slider.js?ver=946207(319B)  
      We are working on site loading performance and would like to get a passing grade, and our load time down to 3 seconds.  We will be making updates to the content size to bring it down as well. Please let me know if there is anything that can be done to address the loading issues found here within PageLines Plugins.
      https://gtmetrix.com/reports/goldenstategreens.com/5rnupbNu
       
      Thanks
    • Christian
      By Christian+
      Hi everyone, since Instagram had allowed to publish in different ratio formats (instead of only square ratio) the grams stripe has different heights. Can you tell me if i can fix it easily with some CSS?
    • 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
×