官术网_书友最值得收藏!

Opening a pop-up window

Pop-up browser windows have received mixed reviews in recent years. Originally created, before tabbed browsers existed, to display additional information without interfering with the page the user had navigated to, they were quickly hijacked and used to display advertisements and spam. Pop-ups should be used sparingly in applications and wherever possible in response to an action by the user.

The target attribute can be specified as _blank on HTML hyperlink tags to open the link in a new window, but all modern browsers allow the user to specify that new windows should be opened as new tabs instead. Also, if the browser does open the URL in a new window, it will be of the same size as the existing window and block most of it. Opening a window in JavaScript allows for fine-grained control over many aspects of the pop-up window, for example, the size and whether to display a toolbar.

In this recipe, we will create a page that renders a list of accounts, displaying a very small subset of fields per row. A link will be provided on each row to allow the user to view full details of the account in a pop-up window.

Note

Note that there is no way to ensure that a browser will display a pop-up window. Pop-up blockers generally allow windows to be opened in response to an action by the user, such as clicking on a link, but it is possible for users to configure their browser to block all pop-ups regardless of how they were triggered.

How to do it...

This recipe requires two Visualforce pages to be created: the main page containing the list of accounts and the pop-up window page. The pop-up page is referenced by the main page, so this will be created first:

  1. Navigate to the Visualforce setup page by clicking on Your Name|Setup|Develop|Visualforce Pages.
  2. Click on the New button.
  3. Enter Popup in the Label field.
  4. Accept the default Popup that is automatically generated for the Name field.
  5. Paste the contents of the Popup.page file from the code download into the Visualforce Markup area.
  6. Click on the Save button to save the page.
  7. Navigate to the Visualforce setup page by clicking on Your Name|Setup|Develop|Visualforce Pages.
  8. Locate the entry for the Setup page and click on the Security link.
  9. On the resulting page, select which profiles should have access and click on the Save button.
  10. Next, create the main account list page by navigating to the Visualforce setup page, clicking on Your Name|Setup|Develop|Visualforce Pages.
  11. Click on the New button.
  12. Enter PopupMain in the Label field.
  13. Accept the default PopupMain that is automatically generated for the Name field.
  14. Paste the contents of the PopupMain.page file from the code download into the Visualforce Markup area.
  15. Click on the Save button to save the page.
  16. Navigate to the Visualforce setup page by clicking on Your Name|Setup|Develop|Visualforce Pages.
  17. Locate the entry for the PopupMain page and click on the Security link.
  18. On the resulting page, select which profiles should have access and click on the Save button.

How it works...

Opening the following URL in your browser displays a list of accounts: https://<instance>/apex/PopupMain.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

Note

Note that, as this page uses a standard list controller, the list of accounts displayed will be that of the last list view that the user accessed.

The detail link markup is as follows:

<apex:outputLink   
    onclick="return openPopup('{!acc.Id}');"> 
    Details 
</apex:outputLink> 

The onclick attribute defines the JavaScript function to be invoked when the link is clicked; note the {!acc.id}merge field, which passes the ID of the chosen account to the function.

The JavaScript function uses the window.open function to open the new window:

var newWin=window.open   ('{!$Page.Popup}?id=' + id, 'Popup',          '   height=600,width=650,left=100,top=100,resizable=no,   scrollbars=yes,toolbar=no,status=no');

The final parameter details the features required for the new window as a comma-separated list of name=value pairs.

Clicking on the Details link displays the full account details in a pop-up window:

See also

  • The Adding a custom lookup to a form recipe in Chapter 3, Capturing Data Using Forms shows how information can be captured in a pop-up window and passed back to the main window to populate input fields.
主站蜘蛛池模板: 石城县| 巧家县| 杂多县| 贵州省| 稷山县| 新泰市| 呈贡县| 临泉县| 修水县| 麻江县| 上饶市| 双桥区| 永靖县| 富阳市| 灵台县| 溧阳市| 宜良县| 延安市| 开阳县| 瓮安县| 眉山市| 伊宁县| 灵石县| 嘉义县| 修文县| 大余县| 通州市| 炎陵县| 商水县| 九台市| 江城| 宝丰县| 改则县| 公安县| 宁乡县| 城市| 尚志市| 金川县| 台南县| 淮北市| 三门峡市|