add fallback if document.currentScript is not available (e.g. with Webkitties)

Fixes #693
Put 'var i' with the initial function var.