Is it possible to create a selection object without any user interaction?

Can a Selection object be created without any user interaction? window.getSelection()returns an object Selection, but you cannot change () if the user does not have his choice.

Is it possible to create a selection that starts with the very first element on the page, and then can make modify()it, without having to do anything?

Example: http://jsfiddle.net/niklasvh/L5M3U/

It doesn’t select anything when the page loads, but if you click on something, you’ll make a choice.

+5
source share
1 answer

, , , addRange(). , <body> , :

function selectBody() {
    var range = document.createRange();
    range.selectNode(document.body);
    var sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
}

window.onload = selectBody;

IE < 9, .

+9

All Articles