Error in Safari: options.length = 0; not working as expected in Safari 4

This is not a real question, but rather an answer to save some other people from the hassle of tracking this nasty bug. I spent a lot of time on this.

When using options.length = 0;to reset all options of a select element in safari, you may get mixed results depending on whether you have a web inspector open or not. If the web inspector is open, you use myElement.options.length = 0;, and after this request options.length()you can return 1 instead of 0 (expected), but only if the web inspector is open (which often happens when debugging such a problem).

Workaround:

Close the web inspector or call myElement.options.length = 0;this twice :

myElement.options.length = 0;
myElement.options.length = 0;

TestCase:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
 <title>Testcase</title>
<script type="text/javascript" language="javascript" charset="utf-8">
function test(el){
 var el = document.getElementById("sel");
 alert("Before calling options.length=" + el.options.length);
 el.options.length = 0;
 alert("After calling options.length=" + el.options.length);
}
</script>
</head>
<body onLoad="test();">
<p>
Make note of the numbers displayed in the Alert Dialog, then open Web inspector, reload this page and compare the numbers.
</p>

<select id="sel" multiple>
 <option label="a----------" value="a"></option>
 <option label="b----------" value="b"></option>
 <option label="c----------" value="c"></option>
</select>
</body>

</html>
+3
3

- myElement.options.length = 0; :

myElement.options.length = 0;
myElement.options.length = 0;
+3

dev-channel. 5.0.375.6

+1

Try:

myElement.options.splice(0, myElement.options.length);

(the first parameter is the starting index, the second parameter indicates how many elements need to be deleted)

0
source

Source: https://habr.com/ru/post/1736644/


All Articles