Complex Javascript question!!!! Please help! + ASP.NET EXPERT question

WarDemon666

Platinum Member
Nov 28, 2000
2,224
0
0
I have a question that Ive been googling for an hour now with no results!

Hopefully someone here can help.


I want to know, if I include a JS file in a html page, does it have access to global variables in the html page it was called from??

ex:

<html>
..
.
var test = "GLOBAL?";

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

</html>

Is it possible to have that variable global for both scripts?

Im dealing with ASP.net and i have to inject javascript into the page to set a variable...

Problem is my code to inject is in a usercontrol, when is inside the content placer of a master page. So, for some dumb reason it never gets executed.

Now im trying to add the var through the header (it adds it right after the header) and the variable gets declared fine, now I just have to find a way to get it to my external JS file....

Ideas?

Thanks in advance!

P.S. Yes, I know im crazy and this is getting complicated for nothing probably....
 

Celeryman

Senior member
Oct 9, 1999
310
0
76
I am not an ASP.NET developer, but as a general rule of thumb, it is not safe (much less secure) at all to print session variables in your client side code. That said, all you would really have to do in your ASP.NET code is something along these lines.

 

Jaxidian

Platinum Member
Oct 22, 2001
2,230
0
71
twitter.com
I don't have Visual Studio in front of me but this is how I would inject such client. Have this ran inside of the CS file for your usercontrol (not the masterpage) and i think this will work. I may have the namespace wrong for the RegisterStartupScript function. Note that there are two different places that has this function but one of them is depricated. The one that you should use takes 2/3 variables:
1. The script in a string.
2. Some unique string - I always pass it this.GetType() (or is it Page.GetType() ?) and that's what msft recommends for the most part.
3. An optional boolean telling it whether or not it should generate your <script> tags for you. I just always have .NET generate them for me so I never have to deal with it or try to remember, "Do I need to put <script> tags in the first variable?".

Good luck!
 

Jaxidian

Platinum Member
Oct 22, 2001
2,230
0
71
twitter.com
Just a little more education about those registerscript functions....

The ones I usually use:
RegisterStartupScript
RegisterOnSubmitScript

The key differences is that the Startup one ensures that the javascript is the last thing rendered in the HTML so you can have javascript that sets initial focus into a textbox or something and be guaranteed that the textbox has already been rendered. I'm not sure how this behaves when you have multiple contentareas inside of a masterpage - not done much with that so if this is the case, be sure to test it. The OnSubmit one acts just like you'd expect, when the OnSubmit event gets fired on the page, your code then runs.

Good luck! :)