Can't use ASP.NET delimiters in a JavaScript include file.

Please see code snippet.

The JavaScript variable tmp1 does not get translated when used within the include file:

<script src="script/test.js" type="text/javascript"></script>

However it does work if I paste the JavaScript directly into a <script tag in main.aspx, where am I going wrong ???


<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

    <script src="script/test.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
      <div id='secMain'></div>
    </form>
</body>
</html>
------------------------------------------------------------------------
test.js:

var tmp1 = '<%=Request.Url.Authority %>';

$("document").ready(function () {

    $("#secMain").html(tmp1);

});






jonatecAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
rename the js file with an aspx extension :

<script src="script/test.aspx" type="text/javascript"></script>

Open in new window

0
 
rajesh_chdCommented:
try this in the .aspx file
<script type="text/javascript" language="javascript">
   var tmp1 = '<%=Request.Url.Authority %>';
</script>
<script src="script/test.js" type="text/javascript"></script>

Open in new window

0
 
jonatecAuthor Commented:
Thanks, but I can get that to work, i need to have

var tmp1 = '<%=Request.Url.Authority %>';

Inside the .js file..
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jonatecAuthor Commented:
leakim971 - ok thanks I'll try that, but is it not possible to use a JS include file then ?
0
 
leakim971PluritechnicianCommented:
<%=Request.Url.Authority %> is .NET code and need to be evaluated by the server in a specific context (your page code with the controls, server variable, memory and so on...)
0
 
rajesh_chdCommented:
no, you can not access .net code in a .js file.
0
 
leakim971PluritechnicianCommented:
you may use ajax/PageMethods to call server side function to get infos/ressourses/data on the server
0
 
jonatecAuthor Commented:
Thanks.
0
 
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.