return $r; } /** * @param int $page 页数 * @param int $pagesize 每页显示数量 * @return mixed */ function link_find($page = 1, $pagesize = 100) { $arr = link__find($cond = array(), array('rank' => -1), $page, $pagesize); return $arr; } /** * @param $id * @return bool 返回FALSE失败 TRUE成功 */ function link_delete($id) { if (empty($id)) return FALSE; $r = link__delete(array('id' => $id)); link_delete_cache(); return $r; } //--------------------------kv + cache-------------------------- /** * @return mixed 返回全部友情链接 */ function link_get($page = 1, $pagesize = 100) { $g_link = website_get('friends_link'); if (empty($g_link)) { $g_link = link_find($page, $pagesize); $g_link AND website_set('friends_link', $g_link); } return $g_link; } // delete kv and cache function link_delete_cache() { website_set('friends_link', ''); return TRUE; } ?>jquery - Page.ResolveUrl is not working in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Page.ResolveUrl is not working in javascript - Stack Overflow

programmeradmin6浏览0评论

In jQuery code I'm using ResolveUrl("~/DynamicMenu.ashx").

But it just returns the exact string:

"ResolveUrl("~/DynamicMenu.ashx")"

Here is a bit of code:

pageIndex = pageIndex + 1;
var CountryCode = getCookie("SetCountry");
var urlToHandler = '<%= ResolveUrl("~/DynamicMenu.ashx") %>'; 
urlToHandler = urlToHandler + CountryCode + "&PageIndex=" + pageIndex;
alert(urlToHandler);

What is wrong in my code for ResolveUrl()?

In jQuery code I'm using ResolveUrl("~/DynamicMenu.ashx").

But it just returns the exact string:

"ResolveUrl("~/DynamicMenu.ashx")"

Here is a bit of code:

pageIndex = pageIndex + 1;
var CountryCode = getCookie("SetCountry");
var urlToHandler = '<%= ResolveUrl("~/DynamicMenu.ashx") %>'; 
urlToHandler = urlToHandler + CountryCode + "&PageIndex=" + pageIndex;
alert(urlToHandler);

What is wrong in my code for ResolveUrl()?

Share Improve this question edited Jan 8, 2014 at 20:20 KyleMit 30.8k72 gold badges511 silver badges702 bronze badges asked Jun 29, 2012 at 14:15 ThomasThomas 34.2k129 gold badges372 silver badges643 bronze badges 3
  • 1 i don't see a javascript call to ResolveUrl. From javascript perspective '<%= ResolveUrl("~/DynamicMenu.ashx") %>' is a string, it is your ASP application that is supposed to run that code – poncha Commented Jun 29, 2012 at 14:20
  • 1 yeah this has nothing to do with javascript. it's asp, and i know i've had problems with server tags in javascript code – Ian Commented Jun 29, 2012 at 14:21
  • <% var siteroot = Url.Content("~/") %> – Thomas Commented Jun 30, 2012 at 17:57
Add a ment  | 

2 Answers 2

Reset to default 2

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:

  • Inline Expressions
  • Embedded Code Blocks
  • Code Nuggets
  • Server Side Script Delimiters
  • Code Render Blocks
  • ASP Directives

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 bining 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):

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

  • What is the difference between ResolveUrl and ResolveClientUrl?
  • Relative Urls

Try this solution - ResolveUrl in Javascript

发布评论

评论列表(0)

  1. 暂无评论