In Javascript, textRange can be created using document.selection. But in fact, it cannot be created at any time.
We can use document.selection.type to determine whether the currently selected text or object is selected.
document.selection.type returns three values "None" "Text" "Control"
Only when the return value is Text, the created textRange is valid.