ResolveUrl in external JavaScript file in asp.net project
https://stackoverflow.com/questions/11263425/page-resolveurl-is-not-working-in-javascript
The problem, as poncha pointed out, is that as far as ASP.NET is concerned, the content delivered in your .js file is a string. It does not apply any sort of rendering before IIS delivers it. It gets the same treatment any other content file would, like a .jpg or .png.
In order to call server side methods (like ResolveUrl), you need to use the <% ... %> syntax within any page that is parsed by ASP.NET (like an .aspx or .master file).
By the way, these little code blocks go by a lot of different names:
In particular, we want a Displaying Expression with the syntax <%= ... %>, where:
the value that is entered after the equals sign is written into the current page
Knowing that, we can build our own own URL by using ResolveClientUrl() which:
returns a URL string suitable for use by the client to access resources on the Web server
To this, we'll pass in the Web Application Root Operator or ~ character, where ASP.NET:
resolves the ~ operator to the root of the current application:
By combining these, we can save the result of the displaying expression into a JavaScript variable by placing the following code on your Master Page (adapted from Joel Varty's blog):
The following script should place before the external JavaScript reference
<script type="text/javascript">
var baseUrl = '<%= Page.ResolveClientUrl("~/") %>';
</script>
Since JavaScript variables are inherently global, any other script can now access the baseUrl variable, so we can utilize it from the .js file with the following script:
function ResolveUrl(url) {
return url.replace("~/", baseUrl);
}
Now you can call ResolveUrl("~/DynamicMenu.ashx") directly from your javascript file and it will create the appropriate URL by stripping out "~/" and replacing it with the baseUrl created earlier by the server side script.
Further Reading:
ResolveUrl in external JavaScript file in asp.net project的更多相关文章
- Dynamically loading an external JavaScript or CSS file
原文: Dynamically loading an external JavaScript or CSS file 通过javascript动态加载css文件和javascript文件,主要是通 ...
- JavaScript应用于asp开发场景
JavaScript应用于asp开发场景 演示代码示例: <%Path="../"%> <!--#include file="../../Inc/Con ...
- 使用 JavaScript File API 实现文件上传
概述 以往对于基于浏览器的应用而言,访问本地文件都是一件头疼的事情.虽然伴随着 Web 2.0 应用技术的不断发展,JavaScript 正在扮演越来越重要的角色,但是出于安全性的考虑,JavaScr ...
- Post Complex JavaScript Objects to ASP.NET MVC Controllers
http://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/ Post ...
- 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法)
此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.Regi ...
- 客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值。
客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值. 无论是什么的html控件,只要加上了runat="server" ...
- AndroidStudio报错:Emulator: I/O warning : failed to load external entity "file:/C:/Users/Administrator/.AndroidStudio3
场景 在进行Android Studio的.Android Studio目录从C盘修改为其他目录后,新建App启动提示: Emulator: I/O warning : failed to load ...
- jquery file upload + asp.net 异步多文件上传
百度了很久,国内一直 找不到 使用jquery file upload 插件 +asp.net 的相关代码 一开始使用 jquery uploadify ,一款基于 flash的插件,但是不支持 Sa ...
- C#和JavaScript交互(asp.net前台和后台互调)总结 (转)
http://www.cnblogs.com/poleices/archive/2011/02/24/1963727.html C#代码与javaScript函数的相互调用: 1.如何在JavaScr ...
随机推荐
- python 3 廖雪峰博客笔记(三) 命令行模式与交互模式
python 的代码一般保存为 .py结尾的文本文件格式 比如 add.py 里写下如下内容 100 + 200 执行 add.py有两种方式: 1. 命令行方式:将python代码写入脚本中执行 p ...
- [LUOGU] P2251 质量检测
题目背景 无 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... Am} ...
- http请求体笔记
一.请求体数据格式 1.application/json:json格式 2.text/plain:纯文本格式 3.application/x-www-form-urlencoded:url编码后产生的 ...
- linux命令 dig-域名查询工具
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常. 语法 dig(选项)(参数) 选项 @<服务器地址&g ...
- nginx配置location项的URL匹配规则
Localtion URL的正则匹配规则 示例 location / { try_files $uri @apache; } #所有的路径都是/开头,表示匹配所有 location @apache { ...
- 经典卷积网络VGG,GoodLeNet,Inception
目录 ImageNet LeNet-5 LeNet-5 Demo AlexNet VGG 1*1 Convolution GoogLeNet Stack more layers? ImageNet L ...
- Blend 混合
Shader 中的混合 Blend Off :不混合 Blend SrcFactor DstFactor :SrcFactor 是源系数,DstFactor是目标系数,源系数是由片段着色器计算出来 ...
- hihoCoder #1162 : 骨牌覆盖问题·三
#1162 : 骨牌覆盖问题·三 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 前两周里,我们讲解了2xN,3xN骨牌 ...
- add favorite & 收藏夹
add favorite // 收藏夹 function favorite (){ var ctrl = (navigator.userAgent.toLowerCase()).indexOf(&qu ...
- Spring Data Jpa系列教程--------实体解析和关联关系
Spring Data Jpa是基于HIbernate开发的,所以建立实体建的实体和映射关系需要好好好的去了解一下,本文有以下内容,实体管理器介绍,实体与数据库表的映射介绍,关联关系(一对多,多对多) ...