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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing vars to a javascript progarmme 2

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
I am not sure how to ask this question but I will give it a try.
I have what I consider to be a standard call to an external javascript program within a dynamically created web page.
Code:
<script type="text/javascript" src="[URL unfurl="true"]http://www.myscript.js"></script>[/URL]

I want to pass some variables into the JavaScript program, I know I can assign global vars but that looks a bit inefficient.
Code:
<script type="text/javascript">var var1 = v1;</script>
<script type="text/javascript">var var2 = v2;</script>

Can this be done in a similar manner to Perl's query process.
eg.
Code:
<script type="text/javascript" src="[URL unfurl="true"]http://www.myscript.js?var1=v1&amp;var2=v2"></script>[/URL]
If so, what is the syntax for recovering the data within the JavaScript program?

I have searched for a solution to this but everything I have found refers to passing vars to functions rather than what I am trying to do.


Keith
 
Server side ---


You GET the values from the URL then 'write' the values in the data stream in a <script> </script> element as 'global' variables


Code:
$val1 = GET('key1')

echo|print "var1=$val1";

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Thanks Chris
I am sure it is simple enough but I don't understand all of your explanation.
I understand how to GET the values from the URL (same as PHP) but I am not sure about the 'write' the values in the data stream, could you please clarify that bit.

Keith
 
Keith

The inefficiency has to exist somewhere. Either you have to add the variables to the query string and then parse the incoming request at the server and spew out the js with the vars within. Or you can just create the variables with the original page load. Or you can load them at some other point in time via ajax.

Personally if I knew what the variables would be at the point I served the page then I'd instantiate them at that time too.

If you are worried about globalising unnecessarily then encapsulate your incoming JavaScript as an object and instead of setting the variables as globals instead instantiate the object and pass through the variables you want. Like a jQuery plugin might do.

 
I would like to pass the vars to the JavaScript via the URL but the searches I have done show some quite complicated solutions to what is a simple job. I have tried some of them but so far have been unsuccessful.
Chris's method above is the way I am used to working with Perl using the CGI module but my attempts at reading in the data to JavaScript have so far failed.
I find being stuck on what should be a simple task very frustrating.

I think I must have misunderstood the GET method, in PHP the var is read directly into a PHP script, is it the same with JavaScript?

Where can I get an example to get me productive again?

Keith
 
apache (etc) typically just treats server side javascript resource as plain text. there is no server side parsing engine for javascript.

what you could do (and I do not recommend this) is to tell apache to send js requests to php.
then at the top of your javascript files include this

Code:
<?php
if(isset($_GET)):
  foreach ($_GET as $var => $val):
    if (is_numeric($val)):
      echo <<<TXT
var $var = $val;
TXT;
   else:
      echo <<<TXT
var $var = "$val";
TXT;
   endif;
  endforeach;
endif;
?>

but better to put known variables into the code via one of the other mechanisms I outlined. the easiest being just to ignore the global/local nature of a variable. namespace them if that makes you feel more comfortable. var myplugin_var1 = 'something';

 
Thanks
I am declaring the vars in the header as globals and this works, I thought there may be a simpler way of adding the vars to the URL but if there is no server side parsing engine, I may just be as well keeping it as it is.

Keith
 
yes. i think that's best.

as said, you can create a server side parsing engine by leveraging php. not ideal.
you can grab the variables via ajax. but why bother if you already know them.

if you're worried about encapsulating, then go the class route, namespace the vars or pass the vars into your code via a function call.

of all of these I'd go with the plugin/object route. but if you don't have much experience in coding js classes then a straight function with sub functions would work as would namespacing (at least to avoid variable clashes anyway).
 
Thanks
I don't think there will be any variable clashes - famous last words!
I have chosen some pretty obscure var names to keep the nosey geeks guessing :)


Keith
 
chris
keith was looking to gt the query params from the src attribute of the script tag.
this is also possible by parsing the tag value.
 
The principle is the same, instead of document.url you can use this.src

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
@Chris indeed. that's why I said it was possible!

@Keith - there is a standard parsing engine. at least within jQuery which is almost a standard now. And you can just split the string on the question mark and then the ampersands and then the equal signs if you want to do it yourself.

 
split the string on the question mark and then the ampersands and then the equal signs if you want to do it yourself.

Or by a regular expression to reduce the processing. string.split(/?|/&)



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top