Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > keeping a persistent reference to an object

Reply
Thread Tools

keeping a persistent reference to an object

 
 
marcadonis@gmail.com
Guest
Posts: n/a
 
      09-17-2005
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-17-2005


wrote:


> 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.



--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
ASM
Guest
Posts: n/a
 
      09-17-2005
wrote:
> 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=\'tr ick\'');
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[i]);
}
receiv()

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

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

--
Stephane Moriaux et son [moins] vieux Mac
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      09-17-2005
wrote:
> 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.


 
Reply With Quote
 
marcadonis@gmail.com
Guest
Posts: n/a
 
      09-18-2005
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!

 
Reply With Quote
 
marcadonis@gmail.com
Guest
Posts: n/a
 
      09-19-2005
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.

 
Reply With Quote
 
marcadonis@gmail.com
Guest
Posts: n/a
 
      09-21-2005
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[i] + '</code>';
var indexOfEq = assignments[i].indexOf('_=');
var key = assignments[i].substring(0, indexOfEq );
var val = assignments[i].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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Persistent field and Persistent properties - difference gk Java 7 10-12-2010 09:43 PM
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Keeping A "Persistent" XML Document In Memory DartmanX Javascript 2 10-12-2005 09:50 PM
Keeping persistent layer (div with style) coordinates ? Guillaume ASP .Net Building Controls 0 08-10-2004 11:01 AM
Web Framework with stateful sessions / persistent object space? Marc Floessel Perl 0 07-09-2004 10:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57