Jump to content

Varaiables in a call for javascript file


waffle72

Recommended Posts

IHi,

 

I wanted to know how to access the variable that you put in call for a javascript file.

In a HTML file I call a Javascript file as follow :

 

<html><head><TITLE>Test clock</TITLE></head><body>
<script src="http://localhost/populationmondiale/test2.js?aff=1&lang=fr&cpop=000000&size=14" type="text/javascript"></script>
<script type="text/javascript">clock();</script></body></html>

 

 

The think is I don't know how to acces the variable

aff=1&lang=fr&cpop=000000&size=14

.

 

If I do a

location.search

it doesn't work cause it gives me the URL of the caller file.

 

Any idea anybody ?

 

Thanks

Waffle

 

 

 

Link to comment
Share on other sites

Well, if you are also using a server-side implementatin (such as PHP) there is a very simple solution. Create your external js files as PHP files (or whatever server-side language you are using).

 

So, let's say this was your (test2.js) file where you were trying to alert() the values on the URL string used to call the file

alert(aff);
alert(lang);
alert(cpop);
alert(size);

 

I left out the code I would have used to grab the variables from the URL because, as you stated, js can't access those values - only the ones from the parent page.

 

So, change that external file to a PHP page such as this:

<?php
echo "var aff = '{$_GET['aff']}'\n";
echo "var lang = '{$_GET['lang']}'\n";
echo "var cpop = '{$_GET['cpop']}'\n";
echo "var size = '{$_GET['size']}'\n";
?>
alert(aff);
alert(lang);
alert(cpop);
alert(size);

 

Then, of course, change the src attribute inthe javascript call to have the php extension.

Link to comment
Share on other sites

Thanks MJ,

 

But I already have the PHP file and I'm triing to do it in Javascript.

 

See, I have a little php routine that is a population counter. I want people to be able to put this counter on there own page. But we have discover recently that some server don't allow allow_url_open. So those poeple can't have an include("http://www....") in there file.

The php routine will still be available but I want to make a jscript routine for those who can't use it.

 

I am sure there is a mean to do that. When you call the Google Map jscript you give parameter so I am sure that there is a way to read them. I just don't know it.

 

<script src="http://maps.google.com/maps?file=api&v=2&key=kjdfhksduj" type="text/javascript">

Link to comment
Share on other sites

Mchl, I don't think that will work in the way the OP wants. It will return the vars for the parent page, not the included JS file.

 

Waffle72,

 

The Google sample youposted is probably doing something similar to what I just proposed. It is calling a page with server-side code that generates the appropriate javascript. Are you hosting the external javascript file? If so, just do as I showed above. It doesn't matter if the calling page is on a server that supports PHP or not.

Link to comment
Share on other sites

Whouaou !

Millions thanks MJ

Yes it works perfectly.

 

I am always forgetting that you can mixte Jscript with php (or the other way around :-) ). I tend to avoid jscript cause it's not portable enough.

 

Anyway for those who are interesting in an answer, here is the call from the caller file. As you can see I had to call my file xxx.php or it doesn't work

 

<script src="http://www.populationmondiale.com/population/clockjs.php?aff=1&lang=fr&cpop=FFFFFF&size=20&cclock=404040" type="text/javascript"></script>

 

and here is a snipset of the clockjs.php file

 

// valeur par defaut des variables
var lang = 'fr'; // language 'fr' or 'en'
var aff = '1'; // affichage '1' - horizontal or '2' - vertical
var size = '10'; // size of the font for the number in pixel
var cpop = 'C0C0C0'; // color of PopulationMondiale.com
var cclock = 'CC0000'; // color of the clock

// ....

<?php
if (isset($_GET['aff']) && !empty($_GET['aff'])) echo "aff = '{$_GET['aff']}'\n";
if (isset($_GET['lang']) && !empty($_GET['lang'])) echo "lang = '{$_GET['lang']}'\n";
if (isset($_GET['cpop']) && !empty($_GET['cpop'])) echo "cpop = '{$_GET['cpop']}'\n";
if (isset($_GET['size']) && !empty($_GET['size'])) echo "size = '{$_GET['size']}'\n";
if (isset($_GET['cclock']) && !empty($_GET['cclock'])) echo "cclock = '{$_GET['cclock']}'\n";
// ...

?>

Link to comment
Share on other sites

I would make one last suggestion. The javascript code that is getting produced will have the variables set twice (assuming the values are sent on the query string). Just create the javascript variables once.

 

Note: you can just use empty() for the test. If the variable is not set, empty() will return true same  as if it was set and had no value.

 

<?php
echo "var aff    = '" . ((empty($_GET['aff']))    ? 'fr'     : $_GET['aff'])    . "';\n";
echo "var lang   = '" . ((empty($_GET['lang']))   ? '1'      : $_GET['lang'])   . "';\n";
echo "var cpop   = '" . ((empty($_GET['cpop']))   ? '10'     : $_GET['cpop'])   . "';\n";
echo "var size   = '" . ((empty($_GET['size']))   ? 'C0C0C0' : $_GET['size'])   . "';\n";
echo "var cclock = '" . ((empty($_GET['cclock'])) ? 'CC0000' : $_GET['cclock']) . "';\n";
?>

Link to comment
Share on other sites

Anyway for those who are interesting in an answer, here is the call from the caller file. As you can see I had to call my file xxx.php or it doesn't work

 

I guess that could be taken care of with mod-rewrite :)

 

???

mod-rewrite ?

 

Isn't that in the htaccess file ?

Link to comment
Share on other sites

I would make one last suggestion. The javascript code that is getting produced will have the variables set twice (assuming the values are sent on the query string). Just create the javascript variables once.

 

Note: you can just use empty() for the test. If the variable is not set, empty() will return true same  as if it was set and had no value.

 

<?php
echo "var aff    = '" . ((empty($_GET['aff']))    ? 'fr'     : $_GET['aff'])    . "';\n";
echo "var lang   = '" . ((empty($_GET['lang']))   ? '1'      : $_GET['lang'])   . "';\n";
echo "var cpop   = '" . ((empty($_GET['cpop']))   ? '10'     : $_GET['cpop'])   . "';\n";
echo "var size   = '" . ((empty($_GET['size']))   ? 'C0C0C0' : $_GET['size'])   . "';\n";
echo "var cclock = '" . ((empty($_GET['cclock'])) ? 'CC0000' : $_GET['cclock']) . "';\n";
?>

 

 

Thanks (again :-) ) MJ

 

I love that cool way of coding the if

Link to comment
Share on other sites

No disrespect Mchl, but using Mod Rewrite to have a particular js file (or all js files for that matter) is a bit overkill. Just making those external files as PHP files (or whatever extension you want to use that is already processed by the server) makes more sense to me.

 

If you were ever to migrate the pages to a new server, that is one thing that would probably get misseed. I have been on too many late night calls during server migrations because of issues like that. My opinion is to keep it simple. In fact, I'd probably name the file something like file.js.php just so it is evident that it is an external js file that is processed by php.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.