Results 1 to 9 of 9

Thread: [RESOLVED] how to open a URL in WebBrowser1 and hit stop before the page loads completely?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Resolved [RESOLVED] how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    The WebBrowser1 control loads a given URL:

    VB Code:
    1. Private Sub Form_Load()
    2. MyURL = "http://www.domain.com/file.html"
    3. WebBrowser1.Navigate MyURL
    4. End Sub

    Now, the file.html contains too much junk (JavaScript, Flash, images, Iframes, etc) that I dont need to be loaded in WebBrowser1. Basically it will take too much time to load the file with all the stuff that comes with it. I can manually click a Command1 and stop the WebBrowser1 from loading it:
    VB Code:
    1. Private Sub Command1_Click()
    2. WebBrowser1.Stop
    3. End Sub
    This gives me exactly what I want, just the file.html loaded into WebBrowser1. What I need is to automate the process without making the user click stop button whenever needed.

    So, the big question is... Is there a way to load the file.html into WebBrowser1 without the images, sound, Flash, and everything else that might come with it, just the file itself?

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    Cache it.

    Two things you can do,



    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Dim sHtml As String
    4.  
    5.     sHtml = Replace$(Replace$(Replace$(Replace$(Replace$(LCase$(Inet1.OpenURL("www.gamespot.com")), "jpg", vbNullString), "swf", vbNullString), "js", vbNullString), "gif", vbNullString), "embed", vbNullString)
    6.     Open "c:\sHtml.html" For Output As #1
    7.     Print #1, sHtml
    8.     Close #1
    9.     WebBrowser1.naviagte "c:\sHtml.html"
    10.  
    11. End Sub
    12.  
    13. Private Sub Command2_Click()
    14.  
    15. Dim sHtml As String
    16.  
    17.     sHtml = Inet1.OpenURL("www.gamespot.com")
    18.     Open "c:\sHtml.html" For Output As #1
    19.     Print #1, sHtml
    20.     Close #1
    21.     WebBrowser1.naviagte "c:\sHtml.html"
    22.  
    23. End Sub

  3. #3
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    that might not be any better as it downloads the file first anyway, try it.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    Thank you for replying.

    I've thought of using Inet1 to download to a local copy first, but the problem is still within the HTML code that the file carries. WebBrowser1 will still open the file as is:
    Code:
    <HTML>
    <HEADE><TITLE>some title</TITLE></HEAD>
    <BODY>
    Some content here blah blah blah ....
    <A HREF="/cool/"><IMG SRC="http://www.site.com/cool.gif">bad image</A><P>
    <UL>
    <LI>example
    <LI>example2
    <PRE>b l a h </PRE>
    </UL>
    <IMG SRC="http://site.com/image.jpg"><BR>example image
    <IMG SRC="http://site.com/image.jpg"><BR>example image
    <IMG SRC="http://site.com/image.jpg"><BR>example image
    <IMG SRC="http://site.com/image.jpg"><BR>example image
    imagine if you get a dozen of these JPGs that wight 500Kb each, but you need to load the page fast, and you dont need to load the images...<P>
    <IMG SRC="http://site.com/image.jpg"><BR>example image
    <IMG SRC="http://site.com/image.jpg"><BR>example image
    <IMG SRC="http://site.com/image.jpg"><BR>example image
    </BODY>
    </HTML>
    the above source code will be passed to the WebBrowser1 if we download the file.html with Inet1.
    Also, it will take forever for WebBrowser1 to figure out that the image links are broken, in case if they are:
    Code:
    <IMG SRC="http://site.com/old-image.jpg"><BR>
    <IMG SRC="http://site.net/broken-link.gif">
    All the problems are fixed when you go:
    WebBrowser1.Stop
    So what I need is to find out when the browser finished loading HTML file, but has not yet started on loading the rest of the content.

  5. #5
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    you can rreplace all img tags with "",



    VB Code:
    1. sHtml = replace$(Replace$(Replace$(Replace$(Replace$(Replace$(LCase$(Inet1.OpenURL("www.gamespot.com")), "jpg", vbNullString), "swf", vbNullString), "js", vbNullString), "gif", vbNullString), "embed", vbNullString),"<img","")

  6. #6
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    was messing with this, works well to remove images,



    VB Code:
    1. Private Sub Command1_Click()
    2. WebBrowser1.Navigate "www.gamespot.com"
    3. End Sub
    4.  
    5.  
    6. Private Sub Timer1_Timer()
    7. On Error Resume Next
    8.   Dim img As MSHTML.HTMLImg
    9.     On Error Resume Next
    10.     For Each img In WebBrowser1.Document.images
    11.       img.src = ""
    12.  
    13.     Next img
    14. End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    Now that looks like what I needed. Thanks Jmacp! Works perfect for the IMG tags, though the page still loads Flash, IFrames, applets, etc.
    This is how GameSpot.com loads flash:
    Code:
        <script type="text/javascript">
            <!-- 
            user_complete = "";
            page_type = "0";
            genre = "0";
            platform = "";
            base_url = "http://www.gamespot.com";
            gne_debug = ((typeof(_smarty_console) == "object") || "0" || "" || "") ? "1":"0";
            
            args = "page_type=" + page_type;
            args += "&genre=" + genre;
            args += "&platform=" + platform;
            args += "&base_url=" + base_url;
            args += "&gne_debug=" + gne_debug;
            args += "&user_complete=" + user_complete;
            
            var MM_contentVersion = 7;
     
            tag = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="302" height="352" id="gs_video" name="gs_video" align="middle">';
            tag += '<param name="allowScriptAccess" value="always" />';
            tag += '<param name="movie" value="http://www.gamespot.com/includes/flash_video/gs_video.swf" />';
            tag += '<param name="quality" value="high" /><param name="wmode" value="transparent" />';
            tag += '<param name="bgcolor" value="#000000" />';
            tag += '<param name="FlashVars" value="' + args + '" />';
            tag += '<embed src="http://www.gamespot.com/includes/flash_video/gs_video.swf" FlashVars="' + args + '" quality="high" bgcolor="#000000" wmode="transparent" width="302" height="352" swLiveConnect=true id="gs_video" name="gs_video" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
            tag += '</object>';
            
            embed(tag);
            
            //ad call
            //http://www.gamespot.com/includes/flash_video/get_ad_xml.php?page_type=0
            
            //-->
        </script>
    Would there be a way to disable JavaScript in the WebBrowser1 too?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    Jmacp, using your example the WebBrowser1 sometimes wont stop loading the page. Some pages load almost instantly, while some pages it takes a lot longer to load without images than with images. It might be a WebBrowser1 bug though.

    I was wondering if we could check the contents of WebBrowser1 for </HTML> closing tag. And we could hit WebBrowser.Stop as soon as we get a </HTML> match. This should prevent loading unneeded content. Though I dont know if this idea is possible to implement at all

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    186

    Re: how to open a URL in WebBrowser1 and hit stop before the page loads completely?

    VB Code:
    1. Private Sub Command1_Click()
    2. WebBrowser1.GoBack
    3. End Sub
    4.  
    5. Private Sub Command2_Click()
    6. WebBrowser1.GoForward
    7. End Sub
    8.  
    9. Private Sub Form_Load()
    10. WebBrowser1.Navigate "http://www.google.com"
    11. End Sub
    12.  
    13. Private Sub WebBrowser1_StatusTextChange(ByVal Text As String)
    14. On Error Resume Next
    15.     Dim ssnvar, re
    16.     ssnvar = WebBrowser1.Document.documentElement.innerHTML
    17.     Set re = New RegExp
    18.     re.Pattern = "</body>"
    19.     re.Global = True
    20.     re.IgnoreCase = True
    21.     If re.Test(ssnvar) = True Then WebBrowser1.Stop
    22. End Sub
    Add references to: Microsoft VBScript Regular Expressions 5.5, and Microsoft Internet Controls

    It navigates to the page, and stops loading the page as soon as WebBrowser1 receives </BODY> tag. One little problem though .... if I click on any link inside the page it wont go there, because the WebBrowser1 already has </BODY> tag loaded, so it stops before loading the new page.

    What would you suggest in this situation?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width