Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using <%= aspnetControl.ClientID %> in javascript 1

Status
Not open for further replies.

brage98

IS-IT--Management
Sep 8, 2010
27
CA
This must be a dead simple one.

When I put my javascript in MyPage.aspx, I can access server controls and variables by putting them inside <%= %>
When I move this javascript out of MyPage.aspx and put it in MyPage.js, <%= %> makes not a lot of sense. What am I doing wrong.


Here's an example of what I'm trying to do:

Code:
//----------------------------------
// MyPage.aspx
//----------------------------------
<html>
<head>
<script type='text/javascript'>
var x = document.getElementById('<%= hidMyVar %>').value;
alert(x)
</scripty>
</head>
<body>
<form>
<input type='hidden' id='hidMyVar' runat='server' />
</form>
</body>
</html>

When I take out the javascript out of the above file, <%= hidMyVar %> throws an exception:

Code:
//----------------------------------
// MyPage.js
//----------------------------------
var x = document.getElementById('<%= hidMyVar %>').value;
alert(x)
//----------------------------------

//----------------------------------
// MyPage.aspx
//----------------------------------
<html>
<head>
<script src="MyPage.js" type="text/javascript"></script>

</head>
<body>
<form>
<input type='hidden' id='hidMyVar' runat='server' />
</form>
</body>
</html>
 
The Js file doesn't get sent through the ASP parser, so neither the ASP code is run, nor the variables replaced by their values.

You have to 2 choices, setup your web server so js files are run through the ASP engine, or simply make every ASP variable set a JS variable before bringing in your JS file. Then your JS variables can use the values received from the ASP code.

Code:
<script type="text/javascript">
var myvar1=<%= hidMyVar %>;
...
</script>
<script src="MyPage.js"></script>




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks for the reply.

I'm trying to keep the aspx page as JS-free as I can.

I like the "set up your webserver" solution.
Question #1:
If JS is parsed through IIS (ASP server), would the user be able to view the code?
Question #2:
How do I go about that? I'm currently using IIS7.0 How would I set it up to get JS files parsed through IIS?
 
Whether its parsed or not, JS is always visible to the user because it has to be visible to the browser. A simple view source is all it takes to view the JS involved.

The ASP engine should not touch the JS, only the ASP present.

Here's a step by step guide on how to step up the IIS server to parse other types of files. It sets it up for html pages, but just change the extension for JS and it should work.

Be warned though that this is not a normal config, so if you plan to host your page on a web server that you can't control, you will not be able to change that setting.

For more IIS questions the forum41 would be a better place.

Also after a bit more research I'm not entirely sure that the ASP in the included file will actually get executed. But there's no harm in trying it out.

Perhaps a better option would be to pass parameters to your functions.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks again for the reply, and the links.
Yes I agree; I think defining the variables before loading up the JS is the better idea. I'll try and see if I can use prototype to make it more clear as to what the JS variables should be prior to including the js in an aspx page.

As for view-source, I think if I include the .js file instead of the actual <script> tag, user won't be able to view the js code. although I'm sure hackers will find a way...

Anyways, thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top