Displaying a number as two digits in JS
("0" + number).slice(-2)
Have fun :)
No title available
Claire Keane
let's talk about Bridgerton tea, my ask is open

Product Placement
No title available
The Bowery Presents

gracie abrams
★

No title available
Mike Driver
PUT YOUR BEARD IN MY MOUTH
EXPECTATIONS
d e v o n

Jar Jar Binks Fan Club
art blog(derogatory)

No title available
Aqua Utopia|海の底で記憶を紡ぐ

ellievsbear

izzy's playlists!
Cosimo Galluzzi
seen from Ukraine
seen from United States

seen from United Kingdom
seen from Brazil
seen from Bangladesh
seen from Türkiye

seen from Singapore

seen from United States

seen from United States

seen from United States
seen from United States
seen from United States
seen from United States

seen from Brazil
seen from T1

seen from Chile
seen from United States

seen from Malaysia
seen from Türkiye
seen from Uzbekistan
@ephigabay-blog
Displaying a number as two digits in JS
("0" + number).slice(-2)
Have fun :)
Allowing same origin requests to your apache
Ever encountered a cross origin error when running something on different ports on your computer?
Well, allowing cross origin requests to your apache is as simple as running this command on your terminal:
sudo ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load
and adding those lines to the `<Directory>` section of your virtual host file (/etc/apache2/sites-enabled/YOUR_SITE_NAME):
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS
Header always set Access-Control-Allow-Headers Origin,X-Requested-With,Content-Type,Accept
Don't forget to restart apache :)
Escaping quotation marks in JS
Escaping quotation marks has weird behavior in JS..
You'd probably expect this to work:
var not_escaped = "Ephi's blog";
var escaped = not_escaped.replace("'", "\\\'");
It's a single quotation mark inside double quotation marks in the first parameter of the replace function
But no, this code does absolutely nothing. Apparently JS treats an escaped quotation mark inside double quotation marks the same as a not escaped one.
So the correct way would be like this:
var not_escaped = "Ephi's blog";
var escaped = not_escaped.replace("'", "\\\\\\\'");
Here's an explanation of why it happens (thanks to Ofir Farchy):
Since a single quotation mark has no escape when it is inside double quotation marks JS just ignores the backslash.
Example:
var something = "\z";
something would be equal to just "z" and JS will ignore the backslash.
Putting some order into AngularJS events
Communicating between controllers in AngularJS is pretty simple, but I think it has poor documentation. So here's a simple explanation:
$scope.$broadcast -> sends the event to child scopes (not siblings)
$scope.$emit -> sends the event to parent scopes (not siblings)
$rootScope.$broadcast -> sends to all scopes
And what is wrong with regular JS events?
?? (double question mark) in JS
Some of you probably remember double question mark from C# which means that if the value is not null return it, otherwise, return the value after the ??
Object a = null;
Object b = new Object();
return a ?? b; // same as: return a == null ? b : a;
So in JS there are no double question mark but, you can use || in order to do the same.
|| will return the first value which does not equal (==) to false, and would not just perform a logical OR.
So you can use it like this:
var a = null;
var b = new Object();
return a || b; // will return b, since a is null
Or even:
var a = null;
var b = null;
var c = new Object();
var d = null;
return a || b || c || d; // will return c
And some more examples:
return parseInt('abc') || 0; // will return 0
return (5 / 0) || 0; // will return Infinity
Changing route in AngularJS without redrawing the ng-view
A colleague of mine had a problem with AngularJS.
She had to change the route of her app without redrawing the ng-view.
The solution was pretty simple, add the following code anywhere inside the controller of the view you don't want to be changed (don't forget to inject $route):
var lastRoute = $route.current; $scope.$on('$locationChangeSuccess', function(event) { $route.current = lastRoute; });
(From this blog post: http://blog.grio.com/2012/08/where-my-docs-at-a-smattering-of-pointers-on-angularjs-one-of-which-at-least-is-difficult-if-not-impossible-to-find-on-the-internet.html)
This code saves the current route in a variable, and every time AngularJS changes the route, this code overrides the current route with the original one, thus causing Angular to think the route was not changed.
Notice, that if you have an "otherwise" in your $routeProvider and the controller that has the code above is outside the ng-view, the "otherwise" would not work. So just put this code inside the controller that controls your route.
document.write a script tag
Ever tried to do something like this:
<script>
document.write("<script src='....'></script>");
</script>
and the browser thought that the closing tag inside the write function is the closing tag for the script?
Do this:
<script>
document.write("<script src='....'></scr"+"ipt>");
</script>