views:

44

answers:

2

My JS works when I reference it in the MasterPage, and it works when I reference it in a Partial View (.ascx), but not when I reference from the View (.aspx). Any ideas why?

A: 

So if you have a block declared in an included file (let's just pretend it looks like)

function alertMe(someValue) {
  alert(somevalue);
}

and in your master page, aspx, and ascx you have

<script type="text/javascript">
  alertMe("some string");
</script>

and it works in the master page and the ascx but not the aspx?

drachenstern
This is not an answer
gustavogb
No, it's a request for clarification of the original question, which included NO code blocks. So you are correct.
drachenstern
+2  A: 

Is the path to the script file correct in your View?

If you inspect HTTP traffic with something like Fiddler or Firebug's Net tab, do you see your script resources being downloaded to the browser?

You might want to use UrlHelper.Content and the relative path to render the script source in the page or perhaps create a HtmlHelper extension method to render out a script tag for you (I think one exists in MVCContrib if you're using that already).

Russ Cam
If the masterpage can call it, and the file is included from the master page, then they should all three be able to call it. but if the block is being included in each place inline, then I would expect it to break.
drachenstern
Re-referencing the same js file is going to cause issues in the future. Law of unintended consequences.
drachenstern
I wouldn't suggest re-referencing the same script twice in a page, as there's the possibility that variable values could be overwritten when the second copy of the script code executes, possibly causing issues. It wasn't clear to me what the OP meant by referencing a script, so I posted a comment to get clarification.
Russ Cam
And yet he already marked the question as answered, and so likely doesn't care anymore.
drachenstern