最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Can i use resolveurl in javascript - Stack Overflow

programmeradmin3浏览0评论
 <script language="javascript" type="text/javascript">
                                                   banner2.add("FLASH", "../Banners/1.swf", 10, 60, 468,"","_blank");
                        banner2.add("FLASH", "../Banners/2.swf", 10, 60, 468,"","_blank");

                    </script>

Now here I want to get the base url of the site so that I can give the path to my flash file in all pages. This script is a part of my master page. Can I run <%= ResolveUrl("~/Banners/1.swf") %> in JavaScript?

banner2.add("FLASH"," <%= ResolveUrl("~/Banners/1.swf") %> ", 10, 60, 468,"","_blank");
 <script language="javascript" type="text/javascript">
                                                   banner2.add("FLASH", "../Banners/1.swf", 10, 60, 468,"http://www.techpint.com","_blank");
                        banner2.add("FLASH", "../Banners/2.swf", 10, 60, 468,"http://www.tapasya.co.in","_blank");

                    </script>

Now here I want to get the base url of the site so that I can give the path to my flash file in all pages. This script is a part of my master page. Can I run <%= ResolveUrl("~/Banners/1.swf") %> in JavaScript?

banner2.add("FLASH"," <%= ResolveUrl("~/Banners/1.swf") %> ", 10, 60, 468,"http://www.techpint.com","_blank");
Share Improve this question edited May 8, 2018 at 19:27 RamenChef 5,55811 gold badges32 silver badges44 bronze badges asked Dec 10, 2009 at 12:07 Shantanu GuptaShantanu Gupta 21.2k56 gold badges186 silver badges293 bronze badges 3
  • yes? What is not working for you? – Johan Wikström Commented Dec 10, 2009 at 12:11
  • error occurs when i try to use ResolveUrl("~/Banners/1.swf") in javascript – Shantanu Gupta Commented Dec 10, 2009 at 14:17
  • I got the solution. We dont have to do ny formating in javascript. I was using escape sequences to write the path. Thx nyway banner2.add("FLASH", "<%= ResolveUrl("~/Banners/1.swf") %>", 10, 60, 468,"techpint.com","_blank"); – Shantanu Gupta Commented Dec 10, 2009 at 14:53
Add a comment  | 

3 Answers 3

Reset to default 8

I got the solution. We dont have to do ny formating in javascript. I was using escape sequences to write the path. Thx nyway

banner2.add("FLASH", "<%= ResolveUrl("~/Banners/1.swf") %>", 10, 60, 468,"techpint.com","_blank";); 

This is something that is super easy, yet I get asked about it quite often.

Here’s how you do it:

In the master page for the site, put this:

<script type="text/javascript">
        var baseUrl = "<%= ResolveUrl("~/") %>";
</script>

Then, in your javascript file, put this function:

function ResolveUrl(url) {
    if (url.indexOf("~/") == 0) {
        url = baseUrl + url.substring(2);
    }
    return url;
}

You could have put the function right in the master page, but then you wouldn’t get intelli-sense on it for the rest of your code.

Now you can call ResolveUrl with ~/ right from javascript.

Super easy, but also super useful!

If you use themes, you might even want to write something that does a “get themed url” where the current theme is output from the master page via Page.Theme.

Source: click me

I think so, as long as your page is being processed by ASP.NET e.g. is not just a static HTML file.

发布评论

评论列表(0)

  1. 暂无评论