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> Code (markup): 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 [COLOR="Red"]myObject [/COLOR]here "<a href="javascript:;" onclick=\"launchCalendar("+[COLOR="Red"]myObject[/COLOR]+")\"> display calendar</a>" + "</body>" + "</html>"; myWindow.document.write(content); myWindow.document.close(); } Code (markup): is this possible to do? I get an error saying: 'missing ] after element list'
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>" + Code (markup): or "<a href='javascript:;' onclick='launchCalendar("+myObject+")'> display calendar</a>" + Code (markup): 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> Code (markup): 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).
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> Code (markup): handlerA.js - opens the pop up window //this is some object I create before opening the window var [COLOR="Red"]myObject [/COLOR]= 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>" + [COLOR="Blue"]"<script language='JavaScript' type='text/javascript' src='handlerB.js'></script>" +[/COLOR] "</head>" + "<body>" + //I want to pass [COLOR="Red"]myObject [/COLOR]here "<a href=\"javascript:;\" onclick=\"launchCalendar("+[COLOR="Red"]myObject[/COLOR]+")\"> display calendar</a>" + "</body>" + "</html>"; myWindow.document.write(content); myWindow.document.close(); } Code (markup): handlerB.js - the file that contains the function 'launchCalendar()' function launchCalendar(calendarObj){ //do something with calendarObj } Code (markup): 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.
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.
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(); } Code (markup):
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.
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(); [COLOR="Red"]var myObject = "";[/COLOR] myArray.push('test'); function Calendar(month){ this.currMonth = month; } function openWindow(){ [COLOR="Red"] var myObject = new Calendar('january');[/COLOR] 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(); } Code (markup): 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!