Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

url.content(...) like method in jquery or javascript? [duplicate]

Possible Duplicate:
Url helper in java script

Url.Content(...) asp.net mvc helper method returns equivalent absolute URL. I'm searching for a method in jquery or javascript that works like this...

because, I want to separat javascript code into a file (.js) and you know that file doesn't supports Url.Content(...) inside javscript code....

url(...) method of jquery not works like Url.Content()

Updated: 22 Jan 2011

I have a workaround:

In the .cshtml file, I created a ‘GetPath’ function that returns absolute path including domain name and can be accessible inside any .js file.

Include following code in any ASP.NET MVC view (.cshtml or .aspx or .vbhtml):

<script type="text/javascript">
    var fullPath = '@HttpContext.Current.Request.Url.Scheme://@HttpContext.Current.Request.Url.Authority';
    function GetPath(url) {
        return fullPath + url;
    }
</script>
<script src="@Url.Content("~/JavaScriptFile.js")" type="text/javascript"></script>

And the code inside any javascript file.

$(function () {
    alert(GetPath('/Content/Site.css'));
});

The result is: http://www.yourDomain.com/Content/Site.css or localhost:1234/Content/Site.css >> Visual Cassini server

You just need to replace @Url.Content("") with GetPath('') in any .js file.

http://muaz-khan.blogspot.com/2012/02/absolute-or-relative-url-issues-and.html

like image 699
Muaz Khan Avatar asked Jan 20 '11 10:01

Muaz Khan


1 Answers

Have a look at ASP.NET MVC JavaScript Routing

like image 86
Giorgi Avatar answered Sep 30 '22 11:09

Giorgi