Hi to all I'm new in the forum and I have the following question:
can I share a javascript object between pages?
I'll explain my self, I have a page that contains two frames:
Currently I can call methods from frames, I was wandering if can retreive the all object, you can see in my example, I can call to the method "CallToit()" that calls to the method CallME() that is defined in the frame2 and return a property of the object, I was wandering I can do something like "window.parent.left.<object name>", or how I can retreive the all object from another frame?

frame1 has:

<script language="javascript" type="text/javascript">
    function CallToIt()
    {        
        var str  = window.parent.frame2.CallME();
        alert(str);
    }
    </script>

frame 2 has:

<script language="javascript" type="text/javascript">
    function ClassObj()
    {
        this.name = name;
        this.setName = setName;
        this.getObjName = getObjName;
        function setName(name)
        {
            this.name = name;
        }
        function getObjName()
        {
            return this.name;
        }
    }
    var testObj = new ClassObj();
    testObj.setName("MyName");
    function CallME()
    {
        return testObj.getObjName();
    }
    </script>

Thanks in advanced

Make that object global in one .js file and include it in both your pages.

gobal.js

var gObject = yourObject;

page1.html

.
.
.
<script src="global.js"></script>
<script>
    gObject = somthing; 
</script>
.
.
.

page2.html

.
.
.
<script src="global.js"></script>
<script>
    var usegObj = gObject; 
</script>
.
.
.

Potato.Head,

JavaScript variables and objects are available to all frames in a frameset with one proviso: the pages loaded into the frames must be served from the same domain. Browsers' security model prevents cross-domain penetration.

Use eg. parent.otherFrame.variable or parent.otherFrame.object.xxx Suggest that you normally won't want to have a copy of data in each frame. This is generally bad as the two might get out of synchronisation with each other. Choose where each variable/object belongs (frame1/frame2) and address accordingly from either the same frame or the other frame.

A typical exception to this "once only rule" is that of functions. It can be convenient for (some) functions (including constructors) to be available to each frame directly. The reason being that each frame may need guaranteed access to a function and there is no guarantee that the other frame is loaded (especially on frameset load). You can jump through hoops making sure things don't fall apart completely.

Hope this helps

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.