开发者

assigning path in Inline javascript in asp.net mvc

开发者 https://www.devze.com 2023-03-30 09:17 出处:网络
I am using MVC 2 architecture. I have a Javacript file (x.js)...which has the declaration for object hs (i.e, var hs).

I am using MVC 2 architecture.

I have a Javacript file (x.js)...which has the declaration for object hs (i.e, var hs).

In my aspx file I am calling (x.开发者_StackOverflow社区js) file using <script> tag and it's running properly.

I am using the hs variable and assigning values to its properties in my aspx in an inline script - find it below:

<script type="text/javascript">
    hs.graphicsDir = '../../Resources/graphics/';
    ...
    ...
</script>

I found this inline script is executing both in my environment and in my publish when i debugged through Firebug.

The problem is, in my environment, the graphicsDir value is taken perfect and executing accordingly, but in the publish the path is not taken properly.

Please suggest how to make it run in the deployment also.


try this

hs.graphicsDir ="<%= Url.Content("~/Resources/graphics")%>"


Following the other correct answer, you can send the correct URL to the client side script using such code in your Page_Load method:

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "correct_url", "var graphDir = \"" + Url.Content("~/Resources/graphics") + "\";", true);

Then in your JS file have this:

hs.graphicsDir = graphDir;

Since it's MVC application not ordinary ASP.NET you have to follow the steps described here to add Code Behind - if you prefer to avoid that, you can also have such code directly in the .aspx file itself:

<script type="text/javascript">
var graphDir = "<%=Url.Content("~/Resources/graphics")%>";
</script>

And have the same code in the .js file as I explained above. (using the global variable)

0

精彩评论

暂无评论...
验证码 换一张
取 消