Link to home
Start Free TrialLog in
Avatar of jonatec
jonatecFlag for United Kingdom of Great Britain and Northern Ireland

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"></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);

});






Avatar of rajesh_chd
rajesh_chd

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

Avatar of jonatec

ASKER

Thanks, but I can get that to work, i need to have

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

Inside the .js file..
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jonatec

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
Avatar of jonatec

ASKER

Thanks.
You're welcome! Thanks for the points!