Skip to content

Javascript knowledge

Rick edited this page Mar 30, 2018 · 5 revisions

JavaScript, check for null, undefined, or blank variables?

if( value ) {
}

will evaluate to true if value is not:

  • null
  • undefined
  • NaN
  • empty string ("")
  • 0
  • false

The above list represents all possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section.

Furthermore, if you do not know whether a variable exists (that means, if it was declared) you should check with the typeof operator. For instance

if( typeof foo !== 'undefined' ) {
    // foo could get resolved and it's defined
}

to debug dynamically loaded JavaScript in browser's dev tools?

You can give your dynamically loaded script a name so that it shows in the Chrome/Firefox JavaScript debugger. To do this you place a comment at the end of the script

//# sourceURL=filename.js

This file will then show in the "Sources" tab as filename.js. In my experience you can use 's in the name but I get odd behaviour if using /'s.


handle URL

There is two ways to handle URL. The first one leverage document.createElement('a') which has good capability with different browsers, even IE6

var url = document.createElement('a')
url.href = "http://www.example.com/some/path?name=value#anchor"
var protocol = url.protocol
var host = url.host
var pathname = url.pathname
var query = url.search
var hash = url.hash

The second one is leverage URL. It's not supported by IE.

var urlString = "http://www.example.com/some/path?name=value#anchor"
var url = new URL(urlString)
url.origin
url.host
url.pathname
url.search
url.hash

When to use escape instead of encodeURI / encodeURIComponent?

escape()

Special characters are encoded with the exception of: @*_+-./

The hexadecimal form for characters, whose code unit value is 0xFF or less, is a two-digit escape sequence: %xx. For characters with a greater code unit, the four-digit format %uxxxx is used.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape

encodeURI()

Use encodeURI when you want a working URL. Make this call:

encodeURI("http://www.example.org/a file with spaces.html") to get:

http://www.example.org/a%20file%20with%20spaces.html Don't call encodeURIComponent since it would destroy the URL and return

http%3A%2F%2Fwww.example.org%2Fa%20file%20with%20spaces.html

encodeURIComponent()

Use encodeURIComponent when you want to encode the value of a URL parameter.

var p1 = encodeURIComponent("http://example.org/?a=12&b=55") Then you may create the URL you need:

var url = "http://example.net/?param1=" + p1 + "&param2=99"; And you will get this complete URL:

http://example.net/?param1=http%3A%2F%2Fexample.org%2F%Ffa%3D12%26b%3D55&param2=99

Note that encodeURIComponent does not escape the ' character. A common bug is to use it to create html attributes such as href='MyUrl', which could suffer an injection bug. If you are constructing html from strings, either use " instead of ' for attribute quotes, or add an extra layer of encoding (' can be encoded as %27).

For more information on this type of encoding you can check: http://en.wikipedia.org/wiki/Percent-encoding

Refer to https://stackoverflow.com/questions/75980/when-are-you-supposed-to-use-escape-instead-of-encodeuri-encodeuricomponent

Clone this wiki locally