Code refactoring, made the function definition related to UA env

Code refactoring, made the function definition related to UA environment, `matchMedia` check only happened once. And some other slight modification for code execution efficiency.