i've created script autoloads javascript files based on users location. other loaders, create <script>
element, set it's src
attribute , add <head>
. listen onload
, onreadystatechanged
etc. events, check presence of value exposed file. works.
the problem is, when supplied invalid cross origin url "http://fail_on_purpose", isp returns html (directing browser reload different address). can catch in onload
event, it's late. html has been injected on page within <script>
tags. causes browser (firefox @ least) issue syntaxerror
exception.
all can remove offending element dom. but, don't know if going cause problems in other browsers, prefer not have error @ all.
what best way check valid javascript file inject page?
update:
issuing head or request check if file exists/is valid, doesn't work due "cross origin" protection in browser. don't have access cdn, unfixable problem.
you send head
request , parse response headers see if page returns successfully, i.e. not redirection, client error or server error response code. decently implemented server should set correct response code.
ideally, you'd check content-type
include application/javascript
, if server set set correctly.
if matches, know script exists, can proceed loading same now.
alternatively, these headers get
request, single request. incorrect request, you'll waiting payload before determining it's incorrect, take more time , waste data transfer. latter worth considering if you're expecting mobile users, wouldn't want waste data plans that.
Comments
Post a Comment