jonatec
asked on
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"></s cript>
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"></s cript>
</head>
<body>
<form id="form1" runat="server">
<div id='secMain'></div>
</form>
</body>
</html>
-------------------------- ---------- ---------- ---------- ---------- ------
test.js:
var tmp1 = '<%=Request.Url.Authority %>';
$("document").ready(functi on () {
$("#secMain").html(tmp1);
});
The JavaScript variable tmp1 does not get translated when used within the include file:
<script src="script/test.js" type="text/javascript"></s
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"></s
</head>
<body>
<form id="form1" runat="server">
<div id='secMain'></div>
</form>
</body>
</html>
--------------------------
test.js:
var tmp1 = '<%=Request.Url.Authority %>';
$("document").ready(functi
$("#secMain").html(tmp1);
});
ASKER
Thanks, but I can get that to work, i need to have
var tmp1 = '<%=Request.Url.Authority %>';
Inside the .js file..
var tmp1 = '<%=Request.Url.Authority %>';
Inside the .js file..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
leakim971 - ok thanks I'll try that, but is it not possible to use a JS include file then ?
<%=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...)
no, you can not access .net code in a .js file.
you may use ajax/PageMethods to call server side function to get infos/ressourses/data on the server
ASKER
Thanks.
You're welcome! Thanks for the points!
Open in new window