473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keeping a persistent reference to an object

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.navigato r, 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

Sep 17 '05 #1
12 5411


ma********@gmai l.com 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.navigato r, 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.variable Name
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.variable Name
as long as the parent document is not reloaded.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 17 '05 #2
ASM
ma********@gmai l.com 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.myI frame.stuff)
parent.myIframe .stuff = 5;
Called page :

if(!!parent.stu ff)
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,attri b,target)
target.location .href=page+'?'+ attrib;
}
</script>
</head>
<body>
<a href="pge1.htm" target="myIfram e"
onclick="send(t his.href,this.t arget,'stuff=5& name=\'foo\'');
return false;">page 1</a>

<a href="pge2.htm"
onclick="send(t his.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.s earch;
attrib = attrib.substrin g(1).split('&') ;
for(var i=0;i<attrib.le ngth;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
Sep 17 '05 #3
ma********@gmai l.com 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.navigato r, 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.
Sep 17 '05 #4
ma********@gmai l.com 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;

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.navigato r, 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!


Howdy

First off , cross-domain javascript references are going to fail,
unless your someone like me who simply proxies pages into my own domain
namespace via server side toys that read the source from any site
via sockets and then pump it out from my own.

As to your navigator object , try self.stuff=5; and then have your pages
reference that value with the full javascript path. (frame included)
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Sep 17 '05 #5
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!

Sep 18 '05 #6
ma********@gmai l.com wrote:
<snip>
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 );


That aint really gonna get it.

You have to keep in mind where the code is actually running
that sets the value , in relation to where the code is actually running
that reads the variable.

lets say I had a frameset something like

<frames>
<frame rows ="50%,50%,*" >
<frame src="pageA.html " name="pageA">
<frame src="pageB.html " name="pageB">
<frame src="blank.html " name="secretfra me">
</frameset>
</frames>

Now as you can see
we have pageA.html that takes up 50% of the screen
we have pageB.html that takes up 50% of the screen
and we have blank.html that takes up the remainder (*) which in this
case is nothing (at least nothing visible)
So from any of these frames I could store values in either the
"top" which is the document holding the three frames, as in
top.stuff=5. If the top frame holding your framest was itself in frames
then that containing frame would become "top", but for pageA,pageB and
secret, there is another name called "parent" which always referes to
the the immeadiately containing frame. I prefere using parent
as opposed to top, as it works no matter how much nesting occurs

Now if pageA ,pageB, or Secret are reloaded the values in top.stuff
or parent.stuff (same thing in your case) will remain, which you can
test with script in any of those pages by setting and getting top.stuff
from any of those frames

you can also reach from pageA to PageB or secret with
parent.pageA.st uff=parent.page B.stuff or parent.pageA.st uff ="foo"

Now if the user reloads the entire framset , your sand castle will
be blown away (variables page and all)

To servive this , there is a place in every document called
document.cookie

The cookie does exactly what you want , in that it will hold a value
in the users browser for hours , days ,months etc , and as soon
as that browser comes back to your page, the document.cookie
will still have that value.
Searching arround will find you some cookie code , or you can snag it
with my DHTML kit in the DHTML section of my site. cokkies
are not exactly as simple as document.cookie ="foo" and require a little
formatting , but even if the user closes their browser , shuts down
their computer, that value will still be their when the boot up and
visit your site.

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Sep 19 '05 #7
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.

Sep 19 '05 #8
ma********@gmai l.com wrote:
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.


I store objects in cookies all the time. One simply
needs to marshal the object to and from a string.

for(i in yourObject){... ..}

iterates through yourObject, and from their you should
be able to figure it out.

If not , check out my DHTML library and look for the functions
DCo2a() and DCa2o()
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Sep 19 '05 #9
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 , shouldMarshallF unctions) {
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' &&
!shouldMarshall Functions)) 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(obje ctAsStr) {
var object = new Object();
var assignments = objectAsStr.spl it(/_\|/);
// var assignments = objectAsStr.spl it(/(?<!\\)\|/); // fails! no
negative look behind assertions in JavaScript regexp
for (var i in assignments) {
document.body.i nnerHTML = '<code>' + assignments[i] + '</code>';
var indexOfEq = assignments[i].indexOf('_=');
var key = assignments[i].substring(0, indexOfEq );
var val = assignments[i].substring(inde xOfEq + 2);

key = key.replace(/\\([=\|\{\}\\])/g, '$1');
val = val.replace(/\\([=\|\{\}\\])/g, '$1');
// alert(key + ' = ' + val);
if (val.indexOf('f unction ') == 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

Sep 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
20384
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But , for example, when the file doesnt exists, i should not return any reference to a bad constructed object, so i need something as a NULL reference object. I suppose i could return a pointer instead, but i have some code written with references which...
10
2278
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template works for many different types. The one that I use is named Integer and is a Wrapper for an int. The...
2
2177
by: John Bowman | last post by:
Hi, I need to sign a specific node in an XML file that contains analytical data & has a structure something like the following: <DOCUMENT> <RESULT_SET> <TESTVAL> </TESTVAL> ...
0
1102
by: Bala | last post by:
I have an ActiveX EXE. One of the methods in this component has "Recordset Object" as input parameter which has to be passed by reference. I have to call this method from C# code. From C#, how do I pass recordset object by reference and use the recordset later in C# Code. Hope my query is clear. Any inputs on this would be greatly appreciated.
3
1519
by: Roger Maynard | last post by:
9
2474
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
3
1844
by: Hugh Oxford | last post by:
I want to build a string to reference an object. I can reference is manually thus: print_r($this->struct->parts->parts); but if I build a string... $string = "->parts->parts"
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.