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的更多相关文章

  1. Dynamically loading an external JavaScript or CSS file

    原文:   Dynamically loading an external JavaScript or CSS file 通过javascript动态加载css文件和javascript文件,主要是通 ...

  2. JavaScript应用于asp开发场景

    JavaScript应用于asp开发场景 演示代码示例: <%Path="../"%> <!--#include file="../../Inc/Con ...

  3. 使用 JavaScript File API 实现文件上传

    概述 以往对于基于浏览器的应用而言,访问本地文件都是一件头疼的事情.虽然伴随着 Web 2.0 应用技术的不断发展,JavaScript 正在扮演越来越重要的角色,但是出于安全性的考虑,JavaScr ...

  4. Post Complex JavaScript Objects to ASP.NET MVC Controllers

    http://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/     Post ...

  5. 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法)

    此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.Regi ...

  6. 客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值。

    客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值.     无论是什么的html控件,只要加上了runat="server" ...

  7. 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 ...

  8. jquery file upload + asp.net 异步多文件上传

    百度了很久,国内一直 找不到 使用jquery file upload 插件 +asp.net 的相关代码 一开始使用 jquery uploadify ,一款基于 flash的插件,但是不支持 Sa ...

  9. C#和JavaScript交互(asp.net前台和后台互调)总结 (转)

    http://www.cnblogs.com/poleices/archive/2011/02/24/1963727.html C#代码与javaScript函数的相互调用: 1.如何在JavaScr ...

随机推荐

  1. 远程连接mysql数据库

    远程连接mysql数据库需要以下步骤(改表法): 1.数据源(数据库服务器)对要访问数据库进行授权: 2.本地库(本地)连接: 具体方法: 1. 进入数据服务器,输入cmd进入控制台:输入 mysql ...

  2. C语言学习8

    计算某日是该年的第几天 编写一个计算天数的程序,用户从键盘输入年.月.日,在屏幕中输出此日期是该年的第几天. /******************************************** ...

  3. 如何设置路由器的MTU

    前几天搞了个ER-X,总觉得没有发挥其最大的能力.今天查了下如何设置MTU,罗列如下,备忘. 1. 目前都是PPPOE,这个不管网络如何复杂,均不要在路由后面计算封包大小.正确的是电脑直接连猫,直接拔 ...

  4. UE4 插件扩展引擎工具栏

    UE4 作为游戏引擎,已经提供了非常强大的游戏开发的API.作为游戏制作者来讲,我们需要一些专用的功能辅助我们更好的开发游戏,而不是仅仅从构建游戏逻辑出发.因此也就有了扩展编辑器功能的这个想法,还好 ...

  5. JS逻辑运算符&&与||的妙用

    JS逻辑运算符&&与||的妙用   /* 文章写的不错 就此分享 */ &&中第一个表达式为假就不会去处理第二个表达式,直接放回结果. || 中就刚很好相反.如果第一个 ...

  6. Open DBDiff 0.9

    SQL Server 迁移过程经常会的出现,需要比对两个数据库之间,或者是表之间到底有何不同 SQL server 自带的tablediff Utility 是一个命令行的工具,对于偶尔需要做一次的体 ...

  7. UI & 原型

    UI & 原型 产品原型设计与团队实时协作平台 https://www.xiaopiu.com/download https://www.xiaopiu.com/forum/posts/5b9 ...

  8. Linux下汇编语言学习笔记11 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. [bzoj4712]洪水_动态dp

    洪水 bzoj-4712 题目大意:给定一棵$n$个节点的有根树.每次询问以一棵节点为根的子树内,选取一些节点使得这个被询问的节点包含的叶子节点都有一个父亲被选中,求最小权值.支持单点修改. 注释:$ ...

  10. codevs——1275 有鱼的声音

    1275 有鱼的声音 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果     题目描述 Des ...