Click to See Complete Forum and Search --> : How do you send variables to linked scripts?


robodan
07-15-2004, 04:40 AM
Dear all,

First of all, let me apologize for sounding a bit on the confused side. I am new to this forum and did not realise that there was a separate JavaScript section. As a result I have gone and posted my original message to the DHTML section, although this could also be said to relate to my question.

Basically, I have made a very minor alteration to a flash detection script that allows it to receive a variable from any page that is linked to it. The idea is that it can then be used to run multiple flash movies of the same dimensions in a ‘css-like’ fashion. To set the variable on the host page I have to declare it with a script tag and then use a separate tag to link to the detection script. Although this works (in a fashion) I am of the thinking that there must be a 'cleaner' way of achieving this.

So far I have looked into several possible solutions including adding a search string to the source attribute of the linking tag, and using the onLoad event handler to set and pass the variable - all to no effect. As I have very limited experience with JavaScript I was wondering if anyone could tell me how variables are usually passed to linked scripts (I have assumed that this must me possible)? If anyone could shed some light on my predicament I would be very grateful, as so far my frantic Googleing has produced only slim results.


For further reference;

http://www.webdeveloper.com/forum/showthread.php?s=&threadid=39438

Kor
07-15-2004, 04:52 AM
I am not very sure what you want... You want to pass a parameter through the address link, similar with a submit? That can be done easily.

robodan
07-15-2004, 05:12 AM
It’s more about economy of code really. So far I have been using this;

<div>
<script><!--
var fNumber = "1";
--></script>
<script src="../javaAndStyles/flash.js" type="text/javascript"></script>
</div>

The ideal would be to add a search string to the second script tags source attribute so that the script could use it as a dynamically generated variable, thus getting rid of the need for two script tags every time I want to use the code. However, I have not managed to find any reference to search strings being used in script tags, and when I try it myself I cannot get the script to register the search strings existence. As I have mentioned previously I had assumed that doing this sort of thing would me common practice (i.e., allowing a script to be used on a ‘pick-&-mix’ basis) but I have absolutely no evidence to back this up.

Kor
07-15-2004, 05:28 AM
You mean... you wanna use in the external script the location as a variable?

something like:

loc=location.href.split('/');
pag=loc[loc.length-1];
if(pag='pag01.html'){...do something}
else if(pag='pag02html'){...do another thing}
else...
...

?

robodan
07-15-2004, 06:10 AM
Mmm, if I understand you correctly that would hard wire the web page to the script (i.e., the script would be looking for the existence of a page rather than a variable on a page). I was looking to use the script for more than one movie on each page. Is there a way of doing the same thing but making the script search for an id attribute instead?

If I can explain, I have been using a div tag to place the movie with css. That's why, so far, I have been looking to use a search string to call the linked script. By placing the linking script tag inside a div it has allowed me to control where on the page the movie got generated. Now, if the script was looking for an id attribute instead, then I could link the script from the pages head section so that it would generate a movie whenever it came across the relevant id’s. Does that sound plausible?

Kor
07-15-2004, 08:24 AM
Aha... so what about inserting a variable when open the new link?

function openP(v){
location.href='newpage.html'+'?'+v
}

...

<a href="#" onclick="openP('var1')>go 1</a>
<a href="#" onclick="openP('var2')>go 2</a>
<a href="#" onclick="openP('var3')>go 3</a>
...

This way the page will be open with a passed parameter (alike with the submit process)
If choose first option (go 1), the function will put in address bar
http://www.yourdomain/newpage.html?var1
The page will be oped normally, that parameter do nothing special

Now, in newpage.html, check which parameter was attached to the self location

function checkVar(){
var url =location.href.split('?');
if(url.length>1){
param=url[url.length-1]
}

In case above, param will have value 'var1'. You can use it further as you wish

robodan
07-15-2004, 01:57 PM
Yes, I can see where you are going with this. However, this does not really do what I am looking for. I can foresee that with this process a site with a lot of pages could potentially get very complicated. Thank you for the brain food though.

I have had another think about what I am actually trying to do and have now come up with a simple answer. In essence all I want to do is run a script that will vary depending on specific parameters that I set on each page. Being a bit slow off the mark I have now realised that what I am referring to is a simple JavaScript function!

By enclosing my script in function brackets and giving it the name ‘whichFlash’, it can be called from anywhere on the page by declaring a parameter. I can call multiple movies on a page with the one script, and even call movies of different sizes by adding other parameters for width and height.

Again, thank you for your help. I think I might start to like using JavaScript after all. :)