web browsing objects

30
Web Browsing Objects

Upload: amandeepbindra

Post on 21-Apr-2015

82 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web Browsing Objects

Web Browsing Objects

Page 2: Web Browsing Objects

Integrating Visual Basic with the Internet

Page 3: Web Browsing Objects

Web Browsing Objects• The two objects weneed in order to add Web techniques and

hyperlinked documents to our Visual Basic applications are: 1. The WebBrowser control2. The InternetExplorer object • The WebBrowser is an ActiveX control that can display HTML

documents on Visual Basic Forms. InternetExplorer is an OLE Automation object that you can use to control Microsoft Internet Explorer (and the WebBrowser control) from within your code.

• The two objects have many common members

Page 4: Web Browsing Objects

The WebBrowser Control• Simply put, the WebBrowser control is Internet Explorer’s

window. • Any HTML document that can be displayed in Internet

Explorer can also be displayed in the WebBrowser control. • In other words, the WebBrowser control adds browsing

capabilities to your Visual Basic applications. It allows the user to browse sites on the World Wide Web, local files, or ActiveX documents, such as Word or Excel documents—all from within a Visual Basic application.

• Because the WebBrowser is an ActiveX control, you can place it on any Visual Basic Form.

Page 5: Web Browsing Objects

Steps to add WebBrowser Control Before you can use it, however, you must add it to the

Toolbox1.Right-click the Toolbox, and from the shortcut menu,

select Components to open the Components dialog box. 2. Select Microsoft Internet Controls, and then click OK.

Two new icons will appear on the Toolbox—the WebBrowser control’s icon and the ShelFolderViewOC control’s icon.

3. Select the WebBrowser control’s icon and draw an instance of the control on the Form.

Page 6: Web Browsing Objects

The WebBrowser Control• When you place a WebBrowser control on a Form, it’s a

borderless rectangle that you can size in any way you like. • Because the control can’t be resized by the user at runtime,

you should try to adjust its size according to the size of its container, which is a Visual Basic Form. When the user resizes the Form, the WebBrowser control should be resized also so that it covers most of the Form.

• To display a Web page in the WebBrowser control, use the Navigate method. You can also move through the list of URLs that have been displayed already with the GoBack and GoForward methods. The WebBrowser control automatically maintains the list of visited URLs.

Page 7: Web Browsing Objects

The WebBrowser Control

Page 8: Web Browsing Objects

The InternetExplorer Object

• The InternetExplorer object allows you to start an instance of Internet Explorer from within your application and manipulate it through OLE Automation.

• The InternetExplorer object supports the same properties and methods as the WebBrowser control, plus a few more.

Page 9: Web Browsing Objects

Steps to add InternetExplorer Control

1. Start a new project and select Standard EXE as the project type. 2. Choose Project Ø References to open the References dialog box:3. Check the Microsoft Internet Controls checkbox.This time, you won’t see a new icon in the Toolbox. But if you open the

Object Browser window, you will see that the InternetExplorer Class has been added to the project.

In the Members window, you will see the properties and methods exposed by the InternetExplorer Class, and through these members you can OLE automate the Internet Explorer application.

Page 10: Web Browsing Objects
Page 11: Web Browsing Objects

The Properties of the WebBrowser Control and the InternetExplorer Object

1. Application This property returns the automation object where HTML documents are displayed (the WebBrowser control or the InternetExplorer object).

2. Busy This property returns a True/False value specifying whether the control is navigating to a new URL or is downloading a Web page. If the control’s Busy property is True for an unusually long time, call the Stop method to cancel the navigation or the download of a document.

3. Container This property returns an object that evaluates to the container of the WebBrowser control.

Page 12: Web Browsing Objects

The Properties of the WebBrowser Control and the InternetExplorer Object

4. Document This property returns the automation object of the active document, if any. This is not the Document object of the Scripting Model. To access the document displayed on the control, you use the following expression:WebBrowser1.Document.Script.Document

5 Height, Width These two properties return the dimensions, in pixels, of the control that contains the WebBrowser control.

6 Top, Left These two properties return the location, in pixels, of the control’s upper-left corner on the Desktop.

Page 13: Web Browsing Objects

The Properties of the WebBrowser Control and the InternetExplorer Object

7. LocationName This property returns the title of the Web page displayed on the WebBrowser control.

8. LocationURL This control returns the URL of the page displayed on the WebBrowser control. The LocationName and LocationURL properties retrieve information about the location of the displayed document. If the location is an HTML page on the World Wide Web, LocationName retrieves the page’s title, and LocationURL retrieves the URL of that page. If the document displayed is a local file, both LocationName and LocationURL retrieve the full path of the file (or its UNC, if it’s located on a network).

9. Type This property returns a string that determines the type of the contained document object. The type for HTML documents is Windows HTML Viewer.

Page 14: Web Browsing Objects

The Methods of the WebBrowser Control and the InternetExplorer Object

1. GoBack, GoForward These two methods navigate backward or forward one item in the history list, which is maintained automatically by the WebBrowser control or the InternetExplorer object. Attempting to move after the most recent URL or before the first URL in the list generates a runtime error. To prevent this, you must include some error-trapping code, similar to the code you saw in the IExplore application, earlier in this chapter.

2. GoHome, GoSearch The GoHome method navigates to the current home page; the GoSearch method navigates to the search page, as specified in the Internet Explorer Options dialog box.

Page 15: Web Browsing Objects

The Methods of the WebBrowser Control and the InternetExplorer Object

3. Navigate This method navigates to a URL or opens an HTML file, as specified in the method’s first argument.This method has the following syntax:

Navigate URL [Flags,] [TargetFrameName,] [PostData,] [Headers]

4. Refresh This method reloads the page currently displayed on the WebBrowser control.

5. Refresh2 This method is similar to Refresh, but it lets you specify the refresh level. It has the following syntax:

Page 16: Web Browsing Objects

The Methods of the WebBrowser Control and the InternetExplorer Object

6. Stop This method cancels any pending navigation or download operation and stops playback of multimedia elements such as background sounds and animations.

Page 17: Web Browsing Objects

The Events of the WebBrowser Control and the InternetExplorer Object

1. BeforeNavigate2 This event occurs when the WebBrowser control is about to navigate to a different URL. It can be caused by external automation (by calling its Navigate method) or by internal automation from within a script or when the user clicks a hyperlink in the current document. Your application has an opportunity to cancel the navigation by setting the method’s Cancel argument to True.

2. NavigateComplete This event occurs after the control has successfully navigated to the new location. Some of the document’s resources may still be downloading (a large image, for instance, may take quite a while), but at least part of the document has been received from the server, and progressive rendering has started already. To interrupt this process, you must call the Stop method.

Page 18: Web Browsing Objects

The Events of the WebBrowser Control and the InternetExplorer Object

3. DownloadBegin This event occurs when a navigation operation is beginning. It’s triggered shortly after the BeforeNavigate event (unless the navigation was canceled), and it signals your application to display a busy message or change the pointer’s shape. The DownloadBegin event has the following declaration:

Sub WebBrowser1_DownloadBegin () 4.DownloadComplete This event occurs when a navigation operation is

finished, halted, or failed. Unlike NavigateComplete, which may not be triggered if the navigation doesn’t complete successfully, this event is always triggered after a navigation starts. Any busy indication by your application must end from within this event. The DownloadComplete event has the following declaration: Sub WebBrowser1_DownloadComplete ()

Page 19: Web Browsing Objects

The Events of the WebBrowser Control and the InternetExplorer Object

5.ProgressChange The WebBrowser control tracks the progress of a download operation and periodically issues the ProgressChange event to inform your application of the progress. The ProgressChange event has the following declaration:

Sub WebBrowser1_ProgressChange(ByVal Progress As Long, _ ByVal ProgressMax As Long)

6.TitleChange This event occurs when the title of the current document changes. The title of an HTML document can change; while the document is being downloaded, the URL of the document is also its title. After the real title (if one was specified with the TITLE tag) is parsed, the TitleChange event is triggered, and you can use it to update the Caption property on your Visual Basic Form.

Page 20: Web Browsing Objects

The Events of the WebBrowser Control and the InternetExplorer Object

The TitleChange event has the following declaration: Sub WebBrowser1_TitleChange(ByVal Text As String) 7. NewWindow Although most hyperlinks result in updating the same

window in which the document with the hyperlink is displayed, some hyperlinks specify that a new window be opened to display the destination document. When a new window is about to be created for displaying a new document, the NewWindow event is triggered.

8. FrameBeforeNavigate, FrameNavigateComplete, FrameNewWindow These three events are identical to the BeforeNavigate, NavigateComplete, and NewWindow events, except that they are triggered from within frames.

Page 21: Web Browsing Objects

Using Document Object

Page 22: Web Browsing Objects

The Document Object• From a programming point of view, the Document object is probably

the most important object in the scripting hierarchy. • The Document object represents the HTML document displayed in the

browser’s window or in one of its frames. Through the Document object’s properties and methods, you can manipulate the appearance and even the contents of the document.

• You can use the bgColor property, for example, to read or set the document’s background color, and you can use the Title property to read the document’s title. You use its Write method to specify the document’s contents from within the script and, in effect, create documents on the fly.

Page 23: Web Browsing Objects

The Properties of the Document Object

1. linkColor, aLinkColor, vLinkColor These properties return or set the color of the links in the document. The linkColor property is the default color of the hyperlinks in the document, aLinkColor is the color of the active hyperlink, and vLinkColor is the color of the hyperlinks already visited. These properties accept color values that can be expressed as hexadecimal numbers or as color names:

• Window.Document.vLinkColor = #00FFFF• Window.Document.linkColor = blue

Page 24: Web Browsing Objects

The Properties of the Document Object

2. bgColor, fgColor These properties return or set the document’s background color and foreground color. The foreground color is the color used for rendering text if the HTML code doesn’t overwrite this setting. Likewise, the background property can be overwritten by the document if it uses a background image. These properties accept color values.

3. Title This property returns the current document’s title. This is a read-only property and can’t be used to change the document’s title at runtime.

Page 25: Web Browsing Objects

The Properties of the Document Object

4. Cookie As you know, scripts written in VBScript are executed on the client computer. VBScript, therefore, had to be a safe language. There is no way for VBScript to access the file system of the client computer and tamper with it. That’s why VBScript lacks the file I/O commands of Visual Basic. A language that can’t store information locally is rather limited. Scripts can’t even open or save a few bytes of data on a local file, and for many applications, this is a serious limitation.

Page 26: Web Browsing Objects

The Properties of the Document Object

5. Anchor Anchor is a property of the Document object, and like some other properties, it is also an object. The Length property of the Anchor object returns the number of anchors in the document. The individual anchors are stored in the Anchors array, whose elements can be accessed with an index. The name of the first anchor in the document is Anchors(0) (its value is the NAME attribute of the <A> tag that inserted the anchor in the document), Anchors(1) is the second anchor, and so on. The following statements display the number of anchors in the current document in a message box:TotalAnchors = Document.Anchors.Length

Page 27: Web Browsing Objects

The Properties of the Document Object

6. Link This property is similar to the Anchor property, but instead of representing the anchors, it represents the hyperlinks in the current document. Like the anchors array, the links array is a property of the Document object, which is the only object that can contain links. The basic property of the Link object is the Length property, which returns the number of links in the document.

Page 28: Web Browsing Objects

The Properties of the Document Object

7. lastModified This property returns the date the current document was last modified. You can use the lastModified property of the Document object to display the date and time it was last modified, without having to hardcode this information in the document itself.

8. Referrer This property returns the URL of the referring document.

Page 29: Web Browsing Objects

The Methods of the Document Object

1. Open This method opens the document for output. The current document is cleared, and new strings can be placed on the document with the Write and WriteLn methods.

2. Write string This method writes the string variable to the document. The argument is inserted in the current document at the current position, but it doesn’t appear until the document is closed with the Close method.

Page 30: Web Browsing Objects

The Methods of the Document Object

3. WriteLn string This method writes the string variable into the current document with a newline character appended to the end. The newline character is ignored by the browser anyway, so the WriteLn string method is practically the same as the Write string method.

4. Close This method closes the document and causes all the information written to it with the Write and WriteLn methods to be displayed, as if it were placed in an HTML document that is loaded in the browser’s window.

5. Clear This method clears the contents of the document.