Solved

Passing parameters to javascript

Posted on 2007-03-24
20
1,244 Views
Last Modified: 2009-07-29
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.
0
Comment
Question by:inexplicable
  • 6
  • 6
  • 5
  • +2
20 Comments
 
LVL 2

Expert Comment

by:fre77
ID: 18784849
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
 
LVL 14

Accepted Solution

by:
B_Dorsey earned 200 total points
ID: 18784871
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
 

Author Comment

by:inexplicable
ID: 18785235
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
 
LVL 25

Expert Comment

by:kevp75
ID: 18785248
B_Dorsey's got it...
0
 

Author Comment

by:inexplicable
ID: 18785288
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
 
LVL 25

Expert Comment

by:kevp75
ID: 18785311
you would request them as you normally would request a querystring in javascript
0
 
LVL 25

Expert Comment

by:kevp75
ID: 18785317
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
 

Author Comment

by:inexplicable
ID: 18785321
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
 
LVL 25

Expert Comment

by:kevp75
ID: 18785339
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
 

Author Comment

by:inexplicable
ID: 18785365
Well, that wouldnt work with a .js file.
For request.querystring, i would need to convert it to into .asp file
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 50 total points
ID: 18785386
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 18785392
and in the js file:

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

or

if  (location.href.indexOf('mysite') !=-1)....
0
 
LVL 14

Expert Comment

by:B_Dorsey
ID: 18785477
@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
 
LVL 14

Expert Comment

by:B_Dorsey
ID: 18785481
@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
 

Author Comment

by:inexplicable
ID: 18785491
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
 
LVL 25

Expert Comment

by:kevp75
ID: 18785498
<%=request.servervariables("HTTP_REFERRER")%>
0
 
LVL 14

Expert Comment

by:B_Dorsey
ID: 18785529
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
 

Author Comment

by:inexplicable
ID: 18785539
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
 
LVL 14

Expert Comment

by:B_Dorsey
ID: 18785570
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
 
LVL 14

Expert Comment

by:B_Dorsey
ID: 18785575
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now