Cursor Position for New Rich Text Element

are you testing in preview mode?

the window object is different in preview and edit mode because the script is executed in different contexts.