Last modified by Pascal Robert on 2010/09/13 00:32

Hide last authors
smmccraw 1.1 1 Opening a link in a new window using JavaScript. There's actually a surprisingly easy way to do it. Just use a WOHyperlink, with an onclick binding.
2
Pascal Robert 3.1 3 {{code}}
smmccraw 1.1 4 Hyperlink3: WOHyperlink {
5 action = myCustomAction;
6 onclick = "return openInNewWindow(this)";
7 target = "_new";
8 }
9
Pascal Robert 5.1 10 {{/code}}
smmccraw 1.1 11
Pascal Robert 3.1 12 {{code}}
smmccraw 1.1 13
Pascal Robert 3.1 14 <script language="javascript">
15 function openInNewWindow(senderLink) {
smmccraw 1.1 16 url = senderLink.href;
17 newWindow = window.open(url,"NewWindow","width=650,height=600,resizable=yes,"+
18 "scrollbars=yes,location=no,toolbar=no");
19 newWindow.focus();
20 return false;
Pascal Robert 3.1 21 }
smmccraw 1.1 22 </script>
23
Pascal Robert 3.1 24 {{/code}}
smmccraw 1.1 25
Pascal Robert 5.1 26 The interesting this is, the hyperlink is sending itself to the openInNewWindow?() function, which then gets the URL from the href property of the link, and opens it in a new window. You can use a regular old action method and bind it to this hyperlink. Also, it will work in browsers that don't have javascript, because the function will not return false, and the link will act like a regular hyperlink whose target is "_new". When the javascript returns false, the link is not followed.
smmccraw 1.1 27
28 To do something a bit more complex, suppose you are storing image dimensions in the database, and want the newly opened window to be exactly the right size. You will need to programatically create the "onclick" binding string for the WOHyperlink, which will pass the dimensions to the openInNewWindow?() javascript function.
29
Pascal Robert 3.1 30 {{code}}
smmccraw 1.1 31
32 Hyperlink3: WOHyperlink {
33 action = myCustomAction;
34 onclick = myOnclickString;
35 target = "_new";
36 }
37
Pascal Robert 3.1 38 {{/code}}
smmccraw 1.1 39
40 and in your component class, add the following method:
41
Pascal Robert 3.1 42 {{code}}
smmccraw 1.1 43
44 public String myOnclickString() {
45 EOEnterpriseObject imageBeingOpened; // assume this exists
46 Integer width = imageBeingOpened.width();
47 Integer height = imageBeingOpened.height();
48 return "return openInNewWindow(this, " + width + ", " + height + ");";
49 }
50
Pascal Robert 3.1 51 {{/code}}
smmccraw 1.1 52
53 And finally, change your javascript method to accept the width & height parameters:
54
Pascal Robert 3.1 55 {{code}}
smmccraw 1.1 56
57 <script language="javascript">
Pascal Robert 3.1 58
smmccraw 1.1 59 function openInNewWindow(senderLink, width, height) {
60 url = senderLink.href;
61 newWindow = window.open(url,"NewWindow","width="+width+",height="+height+",resizable=yes,"+
62 "scrollbars=yes,location=no,toolbar=no");
63 newWindow.focus();
64 return false;
65 }
66
Pascal Robert 3.1 67 </script> 
smmccraw 1.1 68
Pascal Robert 3.1 69
70 {{/code}}