Jump to content

Archived

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

Kedmyster

Problem with custom scripts...

Recommended Posts

Kedmyster

Hey,

 

I'm trying to use a standard function to format numbers with commas within the custom scripts section.

function formatNumber(number, decimals, dec_point, thousands_sep) {
        var n = !isFinite(+number) ? 0 : +number, 
            prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
            sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
            dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
            s = '',
            toFixedFix = function (n, prec) {
                var k = Math.pow(10, prec);
                return '' + Math.round(n * k) / k;
            };
        // Fix for IE parseFloat(0.55).toFixed(0) = 0;
        s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
        if (s[0].length > 3) {
            s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
        }
        if ((s[1] || '').length < prec) {
            s[1] = s[1] || '';
            s[1] += new Array(prec - s[1].length + 1).join('0');
        }
        return s.join(dec);
    }

The problem is with the line that uses the following regular expression:

            s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);

Whenever I publish the changes and refresh, PageLines transforms this line into:

            s[0] = s[0].replace(/B(?=(?:\d{3})+(?!\d))/g, sep);

(the backslash before the B is removed)

Which breaks the whole thing.

 

Tried escaping it with \\\\ but that only postponed to transformation by +1 publish.

 

Is there any solution for this?

 

I tried numerous version of the formatting function, none worked.

 

Thanks!

Share this post


Link to post
Share on other sites
James B

Hi there, someone else reported yesterday that the backslash from the fontawesome code was also being stripped out. I've spoken with the developer team and they've advised its because that area uses stripslashes - http://uk1.php.net/stripslashes

 

To get around this install the Base theme or customize plugin and add your script into the functions.php as a hook.

 

Base theme can be downloaded from here - https://github.com/Dannyholt/Skeleton

 

An example of this can be seen at - http://wordpress.org/support/topic/child-theme-most-eficient-way-of-adding-javascript-to-the-headerphp


Kindly search the forum and read the documentation before posting. It will help you resolve many issues.

For CSS help be sure to check out W3Schools first and be sure to download FireBug for FireFox for troubleshooting.

James B

Share this post


Link to post
Share on other sites
chamois_blanc

When is backslash support in regular expressions going to get fixed? I'm trying to do this myself:

 

        ahref.replace(/^\/#/,'');
 
and the backslash gets removed. Isn't the editor supposed to leave things as is?
 

Share this post


Link to post
Share on other sites
Danny

This isn't a bug and isn't something that is going to be fixed. The DMS editor is supposed to be for simple snippets of CSS/LESS CSS and the same goes for the JS editor too. If you want to include scripts that are not simple scripts such as Analytics etc.. then you're going to need to use a child theme and add those scripts via the WordPress enqueue function.


Please search our forums, before posting!

Share this post


Link to post
Share on other sites

×