简体   繁体   中英

Include a dynamically modified JavaScript file

I would like to include a JavaScript file with razor like this

<script src="@ScriptPath"></script>

Except that I need to change some pathnames dynamically according to @ScriptPath inside the JavaScript before I do. Something like replace all will do.

Can it be achieved and how can it be achieved with razor or/and through controllers?

C# equivalent

    public ActionResult XYZ()
    {
     ViewData["ScriptPath"] = "insert remote link"; or HttpContext.Server.MapPath("~/Scripts/somescript.js");
     return View();
    }

<script src='@ViewData["ScriptPath"]' type='text/javascript'></script>

You should have those paths stored on some database and of course id'ed. And as soon as the page is loaded on the client-side, use Ajax to fetch the paths that are to be within your code by extracting certain identifying strings from the src attribute of the script tag and assigning the retrieved data to certain variables within your code and do whatever you want with that. Again, remember to include certain id's in your path names so that can be useful if you intend to just load particular string snippets that complete or edit the path names.

I hope that was helpful as no coded response is really needed for this kind of question, You seem to have gotten the rest issues figured out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM