best way to use Relative API Url in a jquery Plugin -
so writing jquery plugin using grunt, , in plugin need call webservices.
the webservice url domain same domain location of .js file user needs add our plugin. i.e. relative
1) include js file plugin
2) in js call api url
needs
api_domain: "http://mydomain1/api/v1"
and want api domain relative domain of js file calling tried doing
api_domain: "/mydomain1/api/v1"
but picks domain name of browser.
so wondering best way achieve this
1) use .net handler insert right domain name context.request.url
2) in grunt build create specific .js each environment deploy has full url in js
3) other options ?
well have script find it's own source using jquery. make sure give js file unique enough name.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://rawgit.com/demux/4d4ce440d7c379305a9a/raw/findfilehosttest.js"></script>
actual script:
$(function(){ var url = $("script[src$='findfilehosttest.js']").attr('src') var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i) var domain = matches && matches[1] alert(domain) })
but in general think it's best keep simple. allow user set url of server manually, so:
<script> var my_api_url = 'https://instance1.myapiserver.foo' </script> <script src="/whateverpath/myapiscript.js"></script>
this allow them host script on own server, or proxy ever reason might have.
Comments
Post a Comment