keeping a persistent reference to an object

M

marcadonis

Hi!

Does anybody know of a way that I can keep a reference to an object
that I can then reuse? I tried various approaches using navigator, but
these all fail in an iframe due to premission problems.

For e.g.:

navigator.stuff = 5;

will work fine so long as this is done in the top level window. But if
I then do:

alert(navigator.stuff);

in a child iframe, i get 'undefined'... almost as thought I were
refering to a different navigator object (??). What gives?

Furthermore, if I then try refering instead to top.navigator or
parent.navigator, I get permission denied errors, presumably due to the
fact that my iframe is coming from a different URL than its parent.

There must be some sneaky underhanded way of doing this (using a hidden
frame maybe?), but I'm at my wit's end here!

Help!

Thanks,
Marc
 
M

Martin Honnen

Does anybody know of a way that I can keep a reference to an object
that I can then reuse? I tried various approaches using navigator, but
these all fail in an iframe due to premission problems.

Furthermore, if I then try refering instead to top.navigator or
parent.navigator, I get permission denied errors, presumably due to the
fact that my iframe is coming from a different URL than its parent.

There must be some sneaky underhanded way of doing this (using a hidden
frame maybe?), but I'm at my wit's end here!

With frames/windows from different origins you will always run into the
same origin policy disallowing you access.

If you have a frameset document and frame documents from the same origin
then frames should be able to store stuff in
parent.variableName
as long as the frameset document does not get reloaded.

Same for a HTML document with iframes from the same origin, the iframes
can store stuff in
parent.variableName
as long as the parent document is not reloaded.
 
A

ASM

Hi!

Does anybody know of a way that I can keep a reference to an object
that I can then reuse? I tried various approaches using navigator, but
these all fail in an iframe due to premission problems.

For e.g.:
navigator.stuff = 5;

Main page :

self.stuff = 5;
or :
stuff = 5;
or/and :
if(!!parent.myIframe.stuff)
parent.myIframe.stuff = 5;


Called page :

if(!!parent.stuff)
stuff = parent.stuff;
else
alert('error with \'stuff\' !');


And if that doesn't work, try something as :

Main page :

<html>
<head>
<script type="text/javascript">
function send(page,attrib,target)
target.location.href=page+'?'+attrib;
}
</script>
</head>
<body>
<a href="pge1.htm" target="myIframe"
onclick="send(this.href,this.target,'stuff=5&name=\'foo\'');
return false;">page 1</a>

<a href="pge2.htm"
onclick="send(this.href,'self','stuff=25&name=\'trick\'');
return false;">page 2</a>
</body></html>

Called Page :

<html>
<head>
<script type="text/javascript">

function receiv(){
var attrib = this.location.search;
attrib = attrib.substring(1).split('&');
for(var i=0;i<attrib.length;i++)
eval(attrib);
}
receiv()

if(!!name)
alert('name = '+name);
else
alert('no variable \'name\' or ...\nfunction broken');

</script>
</head>
</html>
 
R

Richard Cornford

Does anybody know of a way that I can keep a reference
to an object that I can then reuse? I tried various
approaches using navigator, but these all fail in an
iframe due to premission problems.

For e.g.:

navigator.stuff = 5;

will work fine so long as this is done in the top level
window. But if I then do:

alert(navigator.stuff);

in a child iframe, i get 'undefined'... almost as
thought I were refering to a different navigator
object (??). What gives?

You are referring to a different - navigator - object. Each
global/window object has its own - navigator - object and each
window/frame/iframe has its own global/window object (interrelated
through the - top - and - parent - references and the - frames -
collection).
Furthermore, if I then try refering instead to
top.navigator or parent.navigator, I get permission denied
errors, presumably due to the fact that my iframe is coming
from a different URL than its parent.

Cross-domain security restrictions. You can get around them if you are
working in different sub-domains but if the desire is to script across
actual domains then you are wasting your time (unless the result is for
an Intranet and you can dictate browser security settings).
There must be some sneaky underhanded way of doing
this

Doing what exactly? Explain the actual requirement (i.e. answer the
question: why?), and in detail.
(using a hidden frame maybe?),
<snip>

If cross-domain security is the problem another frame is unlikely to be
the solution as it can only belong to one domain itself.

Richard.
 
M

marcadonis

First off, many many thanks for all the patient explanations!

Now, just to clarify the issue:
I'm not interested in passing information between the parent and the
child per sei. This was just a method that I had played around with in
order to achieve my goal. The objective here is simply to keep a
reference to an object that stays when the page is reloaded.
I figured the obvious place to assign such a reference would be in the
navigator object. Something like:

alert('before assignment: ' + navigator.stuff);
navigator.stuff = 5;
alert('after assignment: ' + navigator.stuff);

which works fine in the parent (root) window, but fails inexplicably in
the child iframe. That is to say, in the parent, one sees "before
assignment: undefined" and then "after assignment: 5" the first time.
Then on every subsequent visit, one sees "before assignment: 5", since
the value of stuff has been assigned. However, if you try this in the
child iframe, you will always get "before assignment: undefined".

To be sure, I'm not bent on storing reference to stuff in navigator.
Any old place would be fine, just so long as it sticks around.

Thanks again to all!
 
M

marcadonis

Cookies are great for holding strings, but what I need to store is a
reference to an object. This object happens to be a sort of
connection, which must be opened on creation, so unless there's some
way to serialize & deserialize this connection object (which I don't
know), I don't think a cookie is going to cut it for me. Besides, I'm
only interested in keeping this connection around for the life of the
browser.

Thanks anyway for the thoughts.
 
M

marcadonis

I took a look at your DCo2a and DCa2o methods, but they don't seem to
traverse the object tree (unless I'm mistaken). That is, for an
references to other objects it just outputs "[object]". Just for the
record, here's my stab at marshalling/unmarshalling objects:

function marshall(object, shouldMarshallFunctions) {
var objectAsStr = new String();
var isFirstVal = true;
// the _ is to work around the fact that JavaScript regexp seems to
lack negative look-behind assertions
for (var key in object) {
var val = object[key];
var type = typeof(val);
if (type == 'undefined' || (type == 'function' &&
!shouldMarshallFunctions)) continue;
if (!isFirstVal) objectAsStr += '_|';
isFirstVal = false;
if (type == 'object') {
objectAsStr += key + '_=_{' + marshall(val) + '_}';
} else {
val = val.toString();
val = val.replace(/\\/g, '\\\\');
val = val.replace(/([=\|\{\}])/g, '\\$1');
if (type == 'function') objectAsStr += 'function ';
objectAsStr += key + '_=' + val;
}
}
return objectAsStr;
}

function unmarshall(objectAsStr) {
var object = new Object();
var assignments = objectAsStr.split(/_\|/);
// var assignments = objectAsStr.split(/(?<!\\)\|/); // fails! no
negative look behind assertions in JavaScript regexp
for (var i in assignments) {
document.body.innerHTML = '<code>' + assignments + '</code>';
var indexOfEq = assignments.indexOf('_=');
var key = assignments.substring(0, indexOfEq );
var val = assignments.substring(indexOfEq + 2);

key = key.replace(/\\([=\|\{\}\\])/g, '$1');
val = val.replace(/\\([=\|\{\}\\])/g, '$1');
// alert(key + ' = ' + val);
if (val.indexOf('function ') == 0) {
// treat functions
var funcName = key.substring('function '.length);
object[funcName] = eval(val);
} else if (val.indexOf('_{') == 0 && val.indexOf('_}') == val.length
- 2) {
// treat object references
val = val.substring(2, val.length - 2);
val = unmarshall(val);
object[key] = val;
} else {
// treat primitive types
object[key] = val;
}
}
return object;
}


These functions recursively traverse the object tree, as well as
optionally handling the copying of functions.
Anyway... after all that, I still don't think this is going to work.
Turns out my connection object is unexpectedly vast, and it takes far
too much cpu time to serialize it. I'll keep hacking at it anyway.

Cheers,
Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top