Why do I have to escape a space with JavaScript?

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
Post Reply
User avatar
JAB Creations
Posts: 405
Joined: December 28th, 2003, 10:21 pm
Location: Sarasota Florida
Contact:

Why do I have to escape a space with JavaScript?

Post by JAB Creations »

I have the following JavaScript function that I threw together a few days ago that outputs a literal time breakdown based on number of seconds:

Code: Select all

function time_seconds_to_text(s)
{
 //2024-05-15; Return seconds in to a text breakdown:
 var r;

 if (typeof s == 'number')
 {
  var d = Math.floor(s / (24 * 60 * 60));
  s -= d * (24 * 60 * 60);
  var h = Math.floor(s / (60 * 60));
  s -= h * (60 * 60);
  var m = Math.floor(s / (60));
  s -= (m * (60));

  r = ((d > 0) ? d+' day'+((d != 1) ? 's' : '')+',\ ' : '')+((h > 0) ? h+' hour'+((h != 1) ? 's' : '')+',\ ' : '')+((m > 0) ? m+' minute'+((m != 1) ? 's' : '')+' and ' : '')+s+' second'+((s != 1) ? 's' : '');
 }
 else {error_report(new Error('The s parameter was not a valid number type.'));}

 return r;
}
It works fine, that isn't the problem. The problem is: why did I have to escape a space?! It's not a special character! Without escaping the output was:

Code: Select all

time_seconds_to_text(911321)

"10 days,13 hours,8 minutes and 41 seconds"
Then for S&G I added the backslash. I went through all sorts of topics like browser/server/ISP caching and removed compression (just in case there was that one-in-a-decillion chance) all without any changes.

Now, it's working fine with a proper space:

Code: Select all

time_seconds_to_text(911321)

"10 days, 13 hours, 8 minutes and 41 seconds"
Anyone want to take a crack at it and explain why this is even a thing? I'm using Waterfox Current / fully updated (G6.0.14). This is not a life-or-death situation, I simply have never encountered this and want to be better prepared for any similar oddities I (or others who) may come across in the future.
Primarily using Waterfox Current; here to mostly fix the browser's broken GUI. I maintain the Fixed Firefox website to share the things I've confirmed work.
morat
Posts: 6683
Joined: February 3rd, 2009, 6:29 pm

Re: Why do I have to escape a space with JavaScript?

Post by morat »

The code snippet outputs correct spacing for me in the Firefox console after removing the escape characters.

Code: Select all

(function () {
  function time_seconds_to_text(s) {
    ...
  }
  console.log(time_seconds_to_text(911321));
})();
Are you compressing or evaling the code snippet? Sometimes that process causes unexpected problems for me.

Code: Select all

eval("(function() { var s =  '\n';                   })();"); //    error
eval("(function() { var s = '\\n';                   })();"); // no error
eval("(function() { var s = String.fromCharCode(10); })();"); // no error
Uncaught SyntaxError: '' string literal contains an unescaped line break
User avatar
JAB Creations
Posts: 405
Joined: December 28th, 2003, 10:21 pm
Location: Sarasota Florida
Contact:

Re: Why do I have to escape a space with JavaScript?

Post by JAB Creations »

For some reason I can only reproduce this in Waterfox Current but not Firefox 115 ESR with cache disabled and intentionally clearing the cache any way before reloading and retrying. I have a dedicated install and profile per Firefox going all the way back to 1.0 though my platform requires Firefox 44.0+.

I'm not doing anything beyond what I literally posted. My slightly more refined guess is there is going different with how Alex compiles Waterfox Current? I'll link the threads to each other: https://mastodon.social/@Waterfox/112286975130414840.
Primarily using Waterfox Current; here to mostly fix the browser's broken GUI. I maintain the Fixed Firefox website to share the things I've confirmed work.
Post Reply