简体   繁体   English

ASP.NET MVC路由-动态加载.js脚本

[英]ASP.NET MVC routing - loading .js scrips dynamically

Trying to load JavaScript plugin scripts on a _Layout.cshtml page dynamically - from inside a JavaScript file (its from a Bootstrap template). 尝试从JavaScript文件内部(来自Bootstrap模板)在_Layout.cshtml页面上动态加载JavaScript插件脚本。 I have changed the PLUGINS_PATH in the script as follows: 我在脚本中更改了PLUGINS_PATH ,如下所示:

// Declared at top of script
var PLUGINS_PATH = './Content/plugins/';

// ... for each plugin script - e.g
jQuery().themeLoadPlugin(["jPanelMenu/jquery.jpanelmenu.min.js", "jRespond/js/jRespond.js"], [], initjPMenu);

 // Load plugin
 // --------------------------------
 themeLoadPlugin: function(js, css, callback, placement) {
 jQuery.ajaxPrefilter( "script", function( s ) {
     s.crossDomain = true;
 });
 if (js) {
  var progress = 0;
  var internalCallback = function () {
    // Complete
    if (++progress === js.length) {
      $.each(css, function(index, value) {
        jQuery('head').prepend('<link href="' + PLUGINS_PATH + value + '" rel="stylesheet" type="text/css" />');
      });

      if (callback && typeof(callback) === "function") {
        callback();
      }
    }
  };

  if (placement === undefined) {
    $.each(js, function(index, value) {
      $.getScript(PLUGINS_PATH + value, internalCallback);
    });
  }
  else if (placement === 'append') {
    $.each(js, function(index, value) {
      jQuery('script[src*="bootstrap.min.js"]').after('<script src="' + PLUGINS_PATH + value + '"></script>');
      internalCallback();
    });
  }
 }
}

The above works for the index page - eg root of application - http://localhost:1234/ - but if I then if I browse to another page then the links obviously break as the are looking for - eg on the Contact page : http://localhost:1234/Home/Content/plugins/jPanelMenu/jquery.jpanelmenu.min.js 上面的内容适用于索引页面-例如应用程序的根目录http://localhost:1234/ -但如果我然后浏览到另一个页面,则链接显然会因为寻找的内容而中断-例如在“联系”页面上: http://localhost:1234/Home/Content/plugins/jPanelMenu/jquery.jpanelmenu.min.js

If I change the plugins path to : PLUGINS_PATH = '~/Content/plugins/'; 如果我将插件路径更改为: PLUGINS_PATH = '~/Content/plugins/'; then they break on every page with the following: 然后它们在每个页面上显示以下内容:

 http://localhost:1234/~/Content/plugins/jPanelMenu/jquery.jpanelmenu.min.js 

Hope I have explained the issue adequately, any help appreciated. 希望我已经充分解释了这个问题,感谢您的帮助。

The tilde (~) doesn't work in the src attribute of a script tag, you'll just get a literal ~ in the path as you have found. 代字号(〜)在script标签的src属性中不起作用,您只会在找到的路径中得到一个文字〜。 Try: 尝试:

PLUGINS_PATH = '/Content/plugins/'

Using a single dot as you did originally makes the path relative to the current directory, which again as you have found will change as you navigate around. 像您最初那样使用单个点来创建相对于当前目录的路径,再次发现该路径将随您浏览而改变。 When setting the script source: 设置脚本源时:

/ relative to the root /相对于根

./ relative to the directory that contains the current file ./相对于包含当前文件的目录

../ relative to the parent directory of the current directory ../相对于当前目录的父目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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