[thelist] Passing global vars to an iframe

Barney Carroll barney.carroll at gmail.com
Mon Nov 30 10:42:16 CST 2009


Hello list!

Wanted to get comment and advice regarding a method I've developed for
passing global javascript variables from a parent to an iframe. The idea is
to paste variables into a query string and append this to the url of the
iframe, and parse these back on the other side. Host code example:

<iframe src="iframeAd.html" class="passGlobalVars" width="252"
height="252"></iframe>
 <script type="text/javascript">
var queryString, iframes, i;

queryString =
'globalVars&' +
'variableA=' + variableA +
 'variableB=' + variableB +
'&endGlobalVars';

 iframes = document.getElementsByTagName('iframe');

for(i=0;i<iframes.length;i++){
 if(iframes[i].getAttribute('class') &&
iframes[i].getAttribute('class').indexOf('passGlobalVars') > -1){
if(iframes[i].src.indexOf('?') > -1){
 iframes[i].src = iframes[i].src + '&' + queryString;
} else {
 iframes[i].src = iframes[i].src + '?' + queryString;
}
 }
}
</script>

And code in the receiving iframe:

<script type="text/javascript">
var globalVars, i;

if(document.location.href.indexOf('globalVars') > -1){
globalVars =
document.location.href.split('globalVars&')[1].split('&endGlobalVars')[0].split('&');

for(i=0;i<globalVars.length;i++){
if(typeof(window[globalVars[i].split('=')[0]]) === 'undefined'){
 window[globalVars[i].split('=')[0]] = globalVars[i].split('=')[1];
}
 }
}
</script>

I'm wanting to send every global variable to the iframe. Does anyone know of
a non eval-method to round up all global variables and paste their name and
value into a string to define my queryString a bit more programmatically?

Regards,
Barney Carroll

barney.carroll at gmail.com
07594 506 381



More information about the thelist mailing list