Bad Credit Loans - Advertising - Discount Perfume - PT Cruiser - Kamala Harris

PDA

View Full Version : how to pass objects into contents of pop up windows


YourChild
May 8th 2008, 7:16 pm
I have a link which the user clicks and it opens a new window.
inside the new window, there is another link with an onclick event attached.
I want to pass an object which I created prior to creating the window and pass it in the parameter of the 2nd onclick event, which is inside the window

Please take a look at the code it will make more sense.



I have a link like this in html:


<a href="javascript:;" onclick="openWindow()">open window</a>



now here is the javascript that generates the new window:


function openWindow(){
//this is some object I create before opening the window
var myObject = new Calendar();

myWindow = window.open('','mywindow', 'width=380,height=222,resizable=yes,scrollbars=no,toolbars=no,top=50,left=100');

content = "<html>" +
"<head>" +
"<title>welcome to my window</title>" +
"</head>" +
"<body>" +
//I want to pass myObject here
"<a href="javascript:;" onclick=\"launchCalendar("+myObject+")\"> display calendar</a>" +
"</body>" +
"</html>";

myWindow.document.write(content);
myWindow.document.close();
}



is this possible to do? I get an error saying:
'missing ] after element list'

vpguy
May 8th 2008, 11:36 pm
The problem is two-fold. First, you need to escape all of the quotes inside the string:

"<a href=\"javascript:;\" onclick=\"launchCalendar("+myObject+")\"> display calendar</a>" +

or

"<a href='javascript:;' onclick='launchCalendar("+myObject+")'> display calendar</a>" +

But, you also cannot pass an object as a string the way you are doing it. The result of that line of code would produce the following string:

<a href="javascript:;" onclick="launchCalendar([object])">display calendar</a>

You'll need to find some other way to have the popup interact with the opener, such as making the Calendar object a global one in the parent window, and then within the popup you would access it using opener.Calendar (just make sure to check that the opener still exists first, otherwise you'll get errors).

YourChild
May 9th 2008, 1:02 am
well its a little more difficult than it is...at first I thought it was a syntax issue so I tried to simplify the given code...but the situation is more dreading, unfortunately..

there is handlerA.js which opens the pop up window.

when the window opens, it uses another js file, handlerB.js, to take care of business. so my code actually looks like this:

html code:

<a href="javascript:;" onclick="openWindow()">open window</a>



handlerA.js - opens the pop up window


//this is some object I create before opening the window
var myObject = new Calendar();

function openWindow(){

myWindow = window.open('','mywindow', 'width=380,height=222,resizable=yes,scrollbars=no,toolbars=no,top=50,left=100');

content = "<html>" +
"<head>" +
"<title>welcome to my window</title>" +
"<script language='JavaScript' type='text/javascript' src='handlerB.js'></script>" +
"</head>" +
"<body>" +
//I want to pass myObject here
"<a href=\"javascript:;\" onclick=\"launchCalendar("+myObject+")\"> display calendar</a>" +
"</body>" +
"</html>";

myWindow.document.write(content);
myWindow.document.close();
}



handlerB.js - the file that contains the function 'launchCalendar()'

function launchCalendar(calendarObj){
//do something with calendarObj
}


handlerB.js is visible to handlerA.js.
inside handlerB.js, I never touch the window opener though...you said to associate the opener with the calendar object. how do I go about doing this? is mywindow considered as the opener?

pardon me for my lack of experience..I'm still fairly new to js.

vpguy
May 9th 2008, 1:06 am
Okay, I think you're on the right track. If you keep everything exactly as you have it (I noticed you made myObject a global object, which is good), then all you should need to do is change the line in handlerA.js to this:

"<a href=\"javascript:;\" onclick=\"launchCalendar(opener.myObject)\"> display calendar</a>" +

Keep in mind that this doesn't check to make sure that the visitor didn't close the main window and leave the popup open. If so, it will cause an error because opener no longer exists.

YourChild
May 9th 2008, 1:33 am
well it sure is passing through the paramenters...but myObject comes out as undefined in handlerB.js.

Its funny because I am passing myObject and also an array..the array goes through but the object is undefined.

I know myObject is created because I have tested its properties...unless...I'm not creating it correctly?


handlerA.js


var myArray = Array();
var myObject = "";


myArray.push('test');
myObject = new Calendar('january');

function Calendar(month){
this.currMonth = month;
}

function openWindow(){

myWindow = window.open('','mywindow', 'width=380,height=222,resizable=yes,scrollbars=no,toolbars=no,top=50,left=100');

content = "<html>" +
"<head>" +
"<title>welcome to my window</title>" +
"<script language='JavaScript' type='text/javascript' src='handlerB.js'></script>" +
"</head>" +
"<body>" +
//I want to pass myObject here
"<a href=\"javascript:;\" onclick=\"launchCalendar(opener.myObject, opener.myArray)\"> display calendar</a>" +
"</body>" +
"</html>";

myWindow.document.write(content);
myWindow.document.close();
}

vpguy
May 9th 2008, 2:48 am
I'm not sure if this will work, but try replacing these two lines:

var myObject = "";
myObject = new Calendar('january');

with this:

var myObject = new Calendar('january');


Also... make sure the launchCalendar() function can accept two arguments.

YourChild
May 9th 2008, 1:50 pm
I figured out the problem. What happened was, I declared myObject as a global variable and then inside the function openWindow(), I re-declared myObject again...so it was looking like this...


handlerA.js


var myArray = Array();
var myObject = "";


myArray.push('test');


function Calendar(month){
this.currMonth = month;
}

function openWindow(){

var myObject = new Calendar('january');

myWindow = window.open('','mywindow', 'width=380,height=222,resizable=yes,scrollbars=no,toolbars=no,top=50,left=100');

content = "<html>" +
"<head>" +
"<title>welcome to my window</title>" +
"<script language='JavaScript' type='text/javascript' src='handlerB.js'></script>" +
"</head>" +
"<body>" +
//I want to pass myObject here
"<a href=\"javascript:;\" onclick=\"launchCalendar(opener.myObject, opener.myArray)\"> display calendar</a>" +
"</body>" +
"</html>";

myWindow.document.write(content);
myWindow.document.close();
}



my actual code was long so I didnt notice that myObject was being declared again inside the openWindow function... so I took out the redeclaration and now its working. Thanks a lot man..you're a great help. God bless you!