?
Solved

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

Posted on 2011-05-06
9
Medium Priority
?
312 Views
Last Modified: 2012-05-11
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);

});






0
Comment
Question by:jonatec
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:rajesh_chd
ID: 35705810
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
 

Author Comment

by:jonatec
ID: 35706240
Thanks, but I can get that to work, i need to have

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

Inside the .js file..
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 35706474
rename the js file with an aspx extension :

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

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jonatec
ID: 35706590
leakim971 - ok thanks I'll try that, but is it not possible to use a JS include file then ?
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35706628
<%=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
 
LVL 2

Expert Comment

by:rajesh_chd
ID: 35706641
no, you can not access .net code in a .js file.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35706646
you may use ajax/PageMethods to call server side function to get infos/ressourses/data on the server
0
 

Author Closing Comment

by:jonatec
ID: 35727383
Thanks.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35727947
You're welcome! Thanks for the points!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question