fixed error in test algorithm (the loader was OK); fixes #17032
improved algorithm for determining insertion point of script injection; thanks chris2m; fixes #16958; !strict