Passing parameters to javascript

I've an asp page with a javascript file embedded into it. The javascript file is external and its required to capture 2 parameters been passed to it. These parameters i'm getting it from the db. So ideally this is what i would want :

<%
 ' Some asp code outputting results from DB
%><script type="text/javascript" src="loadfile1.js?param1=1&param2=2"></script>

The javascript file doesnt include a function, thats why im not sure how to capture the above 2 parameters and have it displayed into the javascript file.
inexplicableAsked:
Who is Participating?
 
B_DorseyCommented:
jsut write the two parameters on your site before you make your call to the javascript file

<%
 ' Some asp code outputting results from DB
response.write "<script language=""javascript"">" & vblf
response.write "    var param1 = 1" & vblf
response.write "    var param2 = 2" & vblf
response.write "</script>"
%>
<script type="text/javascript" src="loadfile1.js"></script>

This way you can keep your languages seperate and compliant...
0
 
fre77Commented:
1) Rename the .js file to .js.asp (don't forget the links to it )
2) Add asp code in it that captures the parameters (?param1=1&param2=2) and use them as you like

nice and simple...
0
 
inexplicableAuthor Commented:
Fre77, you're solution seems to work out. But its not exactly what i intended..i was actually looking for something that could fetch the parameters in js file without having to convert it into asp. Is that do-able?

Also now in the js.asp file, how can i do a check for the referrer link. Like if someone uses that link on his page, i wouldnt want it to display the result. Only if the request is coming from abc.asp then the result should be displayed from js.asp file. Makes sense ?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
kevp75Commented:
B_Dorsey's got it...
0
 
inexplicableAuthor Commented:
Hmm. But in Dorsey's method, how can i read those 2 parameters under loadfile1.js . Since there's some javascript code that needs access to those 2 parameters.
0
 
kevp75Commented:
you would request them as you normally would request a querystring in javascript
0
 
kevp75Commented:
sorry....you'd need to change B_D's code a bit...

<%
 ' Some asp code outputting results from DB
response.write "<script type=""text/javascript"" src=""loadfile1.js?param1="&variable1FromDB&"&param2="&variable2FromDB&"""></script>"
%>
0
 
inexplicableAuthor Commented:
What good is that from what i'm using already :
<script type="text/javascript" src="loadfile1.js?param1=1&param2=2"></script>

I wanted to know how to read it inside the js file
0
 
kevp75Commented:
DOH!

in your .js file try:
var param1 = new String( Request.QueryString("param1") );
var param2 = new String( Request.QueryString("param2") );

then just use param1 & param2 where you need to in it
0
 
inexplicableAuthor Commented:
Well, that wouldnt work with a .js file.
For request.querystring, i would need to convert it to into .asp file
0
 
Michel PlungjanIT ExpertCommented:
Erm.

Ponder this script

<script type="text/javascript" >
<%
 ' Some asp code outputting results from DB
response.write "    var param1 = 1" & vblf
response.write "    var param2 = 2" & vblf
%>
</script>
<script type="text/javascript" >
function bla() {
  alert(param1)
  alert(param2)
}
window.onload=bla;
</script>


is the same as


<script type="text/javascript" >
<%
 ' Some asp code outputting results from DB
response.write "    var param1 = 1" & vblf
response.write "    var param2 = 2" & vblf
%>
</script>
<script type="text/javascript" src="loadfile1.js"></script>

where loadfile contains
// external js file
function bla() {
  alert(param1)
  alert(param2)
}
window.onload=bla;
// end external...


So you are overcomplicating things.

JS has access to vars in all javascripts. Just to be sure, do not try to access vars in an external javascript until you know it has loaded
0
 
Michel PlungjanIT ExpertCommented:
and in the js file:

if (document.referrer.indexOf('mySite')!=-1) ...

or

if  (location.href.indexOf('mysite') !=-1)....
0
 
B_DorseyCommented:
@mplungjan
This is exactly what I have purposed as my first answer but I dont think the asker understands what is going on.

What everyone else is proposing is kinda useless and causes more server resources (as little as it may be) it still adds more cause now you are now using ASP as your JS interpertor...

I dont know what is hard about this...

IF YOU DECLARE YOUR VARIABLES either with ASP or HTML or JAVASCRIPT or whatever, you can use them in an external JS file as LONG as it is LOADED AFTER the var are declared.



0
 
B_DorseyCommented:
@inexplicable
"Hmm. But in Dorsey's method, how can i read those 2 parameters under loadfile1.js . Since there's some javascript code that needs access to those 2 parameters."

in your loadfile1.js file... jsut call them like

loadfile1.js
  if(param1 == 'test'){
      alert('Look the param before loading the file works')
  }else{
      alert('The param didnt work')
  }
0
 
inexplicableAuthor Commented:
Thanks mplungjan for clarifying the issue. I wasnt really aware of the fact that once a variable is declared in js, it can be used in other js files as well. In other words what you had to say about Dorsey;s code was great.

Thanks Dorsey, your solutions works great. I contradict myself always.:)

Anyways before i close this question, can you let me know about my second query here:

"how can i do a check for the referrer link. Like if someone uses that link on his page, i wouldnt want it to display the result. Only if the request is coming from abc.asp then the result should be displayed from js.asp file. "
0
 
kevp75Commented:
<%=request.servervariables("HTTP_REFERRER")%>
0
 
B_DorseyCommented:
referrer is not dependable, since Norton and McAffee now block your system from sending that data, most sites have gotten rid of using it since its hit or miss.
0
 
inexplicableAuthor Commented:
I see, but basically i wouldnt want anyone from outside my domain to access the .js.asp file. So if i use something like <% if request.servervariables("HTTP_REFERRER")="/abc.asp" %> then only it would process the code else not, ya?

0
 
B_DorseyCommented:
the refferer will only return if they dont have norton or mcaffee installed....

and it will return the whole domain/url not just the script

its going to be hard to stop someone from not from your website from accessing your js file
0
 
B_DorseyCommented:
also I would recommend NOT calling it a js.asp file....

If you have to have it work like an ASP file then change the mime type under IIS and have it interpert it as a ASP file.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.