3

Is it possible to put ASp.NET tags in my javascript which is in a seperate script file. For example, I have the following

 $.getJSON("/Postcode/GetAddressResults/" + $get("SearchPostcode").value, null, function(data) {

which I want to turn into but it does not like the ASP tags!

var action = "<%=Url.Content('~/Postcode/GetAddressResults/')%>" + $get("SearchPostcode").value
        $.getJSON(action, null, function(data) {

However this does not seem to work, what am I doing wrong?

3
  • What is the error you're getting? It is possible to embed asp tags into js since asp is processed first. Commented Mar 30, 2009 at 15:11
  • Any definitive answer as to whether this is possible? I see the workaround below but I would like to know if you can embed the asp.net tags inside script blocks or not. Commented Aug 20, 2009 at 20:03
  • Nevermind -- I didn't read that this was referring to a separate js file. Dynamic tags do work inside of script blocks though it breaks code highlighting and intellisense in Visual Studio. Commented Aug 20, 2009 at 20:37

3 Answers 3

1

Add to your master page(s) a meta tag to hold the value of the current applications path from the host (the bit the ~ represents).

In your Javascript create a function which will resolve a ~ prefixed path using the meta tag content.

Edit

Example as requested:-

Place this code in the head section your master pages:-

  <meta id="meta.AppDomainAppVirtualPath"
    name="AppDomainAppVirtualPath" value="<%=HttpRuntime.AppDomainAppVirtualPath%>" />

In your javascript include this function:-

function resolveUrl(url)
{
    if (url.charAt(0) == "~")
    {
        if (!resolveUrl.appPath)
        {
           var meta = document.getElementById('meta.AppDomainAppVirtualPath');
           resolveUrl.appPath = meta ? meta .getAttribute("content") : '/';
        }

        if (resolveUrl.appPath == '/')
            return url.slice(1, url.length;
        else
            return resolveUrl.appPath + url.slice(1, url.length);
    }
    else
    {
        return url;
    }
}

Now your line of code is:-

$.getJSON(resolveUrl("~/Postcode/GetAddressResults/") + $get("SearchPostcode").value, null, function(data) {
Sign up to request clarification or add additional context in comments.

1 Comment

Can you give me an example of this please?
0

If your JavaScript is in a separate script file, then it won't be processed by ASP.NET, so these tags won't be processed. You'll need them inline in an ASP.NET page for this to work.

Comments

0

No, you can`t. You can expose a path within your view to javascript, but not directly in your external javascript file.

@David M Inlining the whole thing is not really neccesary, you can inline the dynamic part and leave the other part hardcoded.

Regards, Peter

1 Comment

@Peter, If you want to comment on Davids answer then use the Add comment feature. Restrict the contents you your answer to an answer to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.