views:

60

answers:

2
function detailed_link(cell_rowid) {
        var $tabs = $('#infrTab').tabs();
        $tabs.tabs('select', 1); // switch to third tab~
        objRowData = $('#' + pageGridId).getRowData(cell_rowid);
        //document.getElementById("Name").value = objRowData.amount;

        loadPage('Infringement/TaskDetail', 'taskDetails'); /* Path */
    }

I have write a javascript function loadPage(), that needs a path to some page as a parameter. I need to give this path from the application root. I dont want a relative path. Please let me know how can I do it.

A: 

A number of different ways are described in the answers to this similar question.

http://stackoverflow.com/questions/1368264/get-root-url-in-javascript

geoff
+1  A: 

I have this piece of Javascript in my Site.master, just underneath the jquery import and above any reference to my own scripts.

 <script type="text/javascript">
        //Function that provides a relative URL for cases of virtual directories.
        jQuery.url = function (path) {
            return '<%: Url.Action("Index","Home") %>' + path;
        };
 </script>

This assumes that your '/' address is handled by your Index method in your Home controller (the standard).

You can then access it via:

$.url('Infringement/TaskDetail')
Alastair Pitts
Do I have to write jQuery.url = function (path) { return '<%: Url.Action("Index","Home") %>' + path; }; in my javascript file. Or it is internal to jquery.
vaibhav
I personally add it to the Site.Master before MY javascript file as I can then guarantee that it will run before it's ever used.
Alastair Pitts
Its adding a new method to the jQuery object, much like an extension method in .NET
Alastair Pitts
Great idea. However, I'd change the code to something more like this since not everyone has a home index: `return '<%: Url.Content("~") %>' + path;`
mattmc3
@mattmc3: That's a much cleaner version that mine :)
Alastair Pitts