View Full Version : List all global variables
Danne
08-26-2003, 11:34 PM
Is there a way to list all global variables from a frame?
I tried this:
var arr=[]
for (var prop in window){
arr[arr.length]=prop;
}
alert( arr.join("\n") );
But it only lists the predefined properties of window; document, frames, etc...
Any ideas?
I'm using IE6.0, btw, but it would be good if it works in Mozilla too.
Graeme Hackston
08-27-2003, 12:30 AM
Sorry, as far as I know, no. You can in Mozilla but I think it's not possible in IE.
Graeme Hackston
08-27-2003, 12:58 AM
Sorry I didn't read your question properly.
Regarding Moz you can get the parents global variables and functions with your code but it also lists all the built in functions. Does it not work for a child frame on the same domain?
Also, this gets you a little more info but I don't see a way to separate the built ins from globals.
for (i in window) {
document.write('Property = ' + i + '<br>Value = ' + window[i] + '<p></p>')
}
Graeme Hackston
08-27-2003, 01:21 AM
I guess you can get functions in Moz like this but I can't see a way to get global variables.
for (i in window) {
if (typeof window[i] == 'function' && !window[i].toString().match(/native code/))
document.write('Property = ' + i + '<br>Value = ' + window[i] + '<p></p>')
}
cheesebag
08-27-2003, 02:06 AM
Neither IE (nor Opera, I believe) enumerates user-defined 'global' variables, although they are essentially window members.
Numerous properties of other host objects are also not enumerable.
var x = 'global';
alert(window.x);
Have no idea why. :confused:
http://www.webreference.com/js/column80/6.html
Danne
08-27-2003, 03:39 PM
OK, thanks guys. I have to find a different solution for IE.
Maybe identify them using document.scripts[0].innerHTML?
Vincent Puglia
08-27-2003, 10:27 PM
Hi Danne,
<script type="text/javascript">
<!--
var x = 3;
var y = 'global'
//-->
</script>
<script language="javascript">
x--;
</script>
</head>
<body>
<script language="javascript">
var x =+ 1;
for (i = 0; i < document.scripts.length; i++)
alert('script ' + i + ' = ' + document.scripts[i].innerHTML )
</script>
Vinny
Graeme Hackston
08-28-2003, 01:23 AM
I stumbled on scripts at MSDN a few weeks ago and played with it for a bit. I didn't get anywhere and wrongly assumed it was useless for this type of situation. Thanks for the lesson Vincent.
Danne
08-28-2003, 05:51 PM
Yes, Vincent, that's what I'm working with. I'd like to copy all script from an iframe to the main window. This works:
var str='';
var d = window.frames["iFrame"].document;
var scripts = d.getElementsByTagName("script");
for (i = 0; i < scripts.length; i++)
str+='<script>'
+ scripts[i].innerHTML
+ '<\/script>';
document.write(str);
I can now call the functions in the window, but the existing document content will disappear/be deleted.
If I have js-includes (in other files) I can use the src, which works too:
var d = window.frames["iFrame"].document;
var scripts = d.getElementsByTagName("script");
for (i = 0; i < scripts.length; i++){
var newScript=document.createElement('SCRIPT');
newScript.src=scripts[i].src;
document.getElementByTagName('HEAD')[0].appendChild(newScript);
}
The problem is this doesn't work:
var d = window.frames["iFrame"].document;
var scripts = d.getElementsByTagName("script");
for (i = 0; i < scripts.length; i++){
var newScript=document.createElement("SCRIPT");
newScript.innerHTML=scripts[i].innerHTML; // Error
document.getElementByTagName('HEAD')[0].appendChild(newScript);
}
And neither does this:
var d = window.frames["iFrame"].document;
var scripts = d.getElementsByTagName("script");
for (i = 0; i < scripts.length; i++){
str='<scr'+'ipt>';
str+=scripts[i].innerHTML;
str+='<\/scr'+'ipt>'
//document.body.innerHTML+=str;
document.getElementById('divId').innerHTML=str;
}
In the last case, the div.innerHTML has the script tag and code inside, but the functions doesn't respond. Maybe because the browser didn't compile them or something like that.
I can copy the the globals if I have a list of all globals, as long as they aren't instances of object (In that case they will reference to the wrong frame). For now I solved that with a loop and a bunch of RegExp's processing the scripts[i].innerHTML to identify the globals.
Vincent Puglia
08-28-2003, 07:05 PM
Hi Danne,
Based partially on the following, I would suspect you cannot append a script to the head:
x = document.getElementsByTagName('head')[0]
x.isContentEditable = true; // the default is false
for (i in x)
document.write(i + ' ' + x[i] + '<br>')
Also, you might find the following interesting:
var vp;
vp = document.createElement("script");
alert(vp.outerHTML)
alert(vp.innerHTML)
alert(vp.length)
Out of curiousity, why do you want to change/append scripts on the fly?
Vinny
glenngv
08-29-2003, 04:21 AM
I'd like to copy all script from an iframe to the main window.
Why? You can reference the global variables and functions in a frame from another frame.
var i = top.frames["iFrame"].someGlobalVariable;
var j = top.frames["iFrame"].someFunction();
Vincent Puglia
08-29-2003, 03:28 PM
Hi glenn,
You can reference the global variables and functions in a frame from another frame.
This presupposes that you know the names of the variables and functions.
Danne:
I played with it some more last night (even adding the 'newscript' to the scripts array much as if it were any other array -- no success. So again, why do you want to modify the existing scripts?
Vinny
Danne
08-29-2003, 03:35 PM
Originally posted by Vincent Puglia
would suspect you cannot append a script to the head
Well, actually it is possible to do so. As i wrote before, this works:
var d = window.frames["iFrame"].document;
var scripts = d.getElementsByTagName("script");
for (i = 0; i < scripts.length; i++){
var newScript=document.createElement('SCRIPT');
newScript.src=scripts[i].src;
document.getElementByTagName('HEAD')[0].appendChild(newScript);
}
Why do I want to do this?
I have developed a kind of clientside controller for a site that administrates recipes. This includes many different data arrays which would be heavy to download as html. So the main idea with the controller is to only download data and html structures once only. Everything is mounted on clientside. This part is fully developed and works fine. The problem is that it is slightly burocratic to implement if the page is static. So to fit a normal page in this structure it's necessary to load the page in a hidden iframe, and then copy it to the main document.
Danne
08-29-2003, 04:45 PM
Originally posted by Vincent Puglia
Danne:
I played with it some more last night (even adding the 'newscript' to the scripts array much as if it were any other array -- no success. So again, why do you want to modify the existing scripts?
Vinny
Thanks for trying anyway :)
I posted the answer above. And if you come up with something, I'm all ears..
glenngv
09-01-2003, 04:07 AM
Originally posted by Vincent Puglia
Hi glenn,
This presupposes that you know the names of the variables and functions.
Vinny
Yes, but how would you use the scripts if you don't know what variables to use, what functions to call?
So I still think, with Danne's explanation, that you don't have to copy the script from a hidden frame. Just call the functions and variables directly from a frame. Why would you load the script twice?
Danne
09-02-2003, 02:16 PM
Originally posted by glenngv
Yes, but how would you use the scripts if you don't know what variables to use, what functions to call?
The thing is, I copy the whole page - not just the script, but also the HTML. Since the scripts are called from the HTML, it's not necessary to know the name of the function while doing this. That is defined in the HTML.
Originally posted by glenngv
So I still think, with Danne's explanation, that you don't have to copy the script from a hidden frame. Just call the functions and variables directly from a frame. Why would you load the script twice?
Considering this, I think my solution is not very good. The pages in the system part of the site are shown inside divs, but maybe the simple pages should be shown inside iframes.
glenngv
09-03-2003, 07:38 AM
Originally posted by Danne
The thing is, I copy the whole page - not just the script, but also the HTML. Since the scripts are called from the HTML, it's not necessary to know the name of the function while doing this. That is defined in the HTML.
Do you mean you have 2 frames with exactly the same page? I believe that the source frame is a hidden frame.
Why don't you just put the same url on that frame if that's the case?
in the destination frame:
<head>
<script>
location.replace(top.frames["nameOfSourceFrame"].location.href);
</script>
</head>
Danne
09-03-2003, 03:46 PM
Originally posted by glenngv
Do you mean you have 2 frames with exactly the same page? I believe that the source frame is a hidden frame.
Not exactly. I have a controller in the main frame, that I don't want to unload it. So loading the new page in an iframe and then copy it to the main frame was a pretty bad idea, but it seemed to make sence at the time..:rolleyes: I decided to show the iframe when a page like that is loaded. That will keep it separated from the rest of the site.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.