javascript - Load a particular JS file base on what URL its hosted -
i've mvc asp page following code:
<script type="text/javascript" src="https://test_domain.com/test.js"> </script> @*<script type="text/javascript" src="https://live_domain.com/live.js"> </script>*@
essentially comment or uncomment script want use if site live or if on test. i'd love able dynamically how. 1 solution, perhaps, if read current url , figure out if live/test.
updated answer (thanks whipdancer)
in web.config have added url:
<add key="bundlejs" value="https://test_domain.com/test.js" /> <!--<add key="bundlejs" value="https://live_domain.com/live.js" />
i web.config transformations next. better had before.
the next step during session_start in global.asax.cs file set url application variable:
application["bundlejs"] = system.configuration.configurationmanager.appsettings["bundlejs"];
after set able go controller of view (tip: in case view layout went parent controller). on or after actionresult method added application variable viewbag
viewbag.bundlejs = httpcontext.application["bundlejs"];
finally in cshtml page (which _layout me) set script up
<script type="text/javascript" src="@viewbag.bundlejs"> </script>
since using asp.net mvc - can use web config transformations handle different environments.
more info on web config transformations
i use web config parameter determine appropriate environment, loaded via global.asax (or possible in primary view controller).
you able automatically determine appropriate url based on environment compiled to.
in test web.config: <appsettings> <add key="jssource" value="https://test_domain.com/test.js" /> </appsettings> in release web.config: <appsettings> <add key="jssource" value="https://live_domain.com/live.js" /> </appsettings>
in global.asax like:
public class mvcapplication : system.web.httpapplication { public static string jsurl; protected void application_start() { jsurl = system.configuration.configurationmanager.appsettings["jssource"]; } }
in page use this:
<script type="text/javascript" src="@jsurl"> </script>
** don't think code run is. give general idea.
Comments
Post a Comment