INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Passing vars to a javascript progarmme

Passing vars to a javascript progarmme

(OP)
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="http://www.myscript.js"></script> 

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="http://www.myscript.js?var1=v1&var2=v2"></script> 
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
www.studiosoft.co.uk

RE: Passing vars to a javascript progarmme

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 --> pseudo

$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

RE: Passing vars to a javascript progarmme

(OP)
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
www.studiosoft.co.uk

RE: Passing vars to a javascript progarmme

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.

RE: Passing vars to a javascript progarmme

(OP)
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
www.studiosoft.co.uk

RE: Passing vars to a javascript progarmme

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';

RE: Passing vars to a javascript progarmme

(OP)
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
www.studiosoft.co.uk

RE: Passing vars to a javascript progarmme

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).

RE: Passing vars to a javascript progarmme

(OP)
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
www.studiosoft.co.uk

RE: Passing vars to a javascript progarmme

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.

RE: Passing vars to a javascript progarmme

(OP)
Is there no standard parsing engine for a reason or is something most authors have no use for?

Keith
www.studiosoft.co.uk

RE: Passing vars to a javascript progarmme

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

RE: Passing vars to a javascript progarmme

@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.

RE: Passing vars to a javascript progarmme

Quote:

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close