Results 1 to 11 of 11

Thread: [RESOLVED] Open webpage in existing tab of existing browser

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Resolved [RESOLVED] Open webpage in existing tab of existing browser

    I have a links program that has a ton of links on it. The idea is to click a link and have it open that page in the default browser. It should cannibalize the browser window if one is already open. So far so good; this works fine:
    Code:
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    
    ' Launch default browser
    Public Sub OpenURL(ByVal URL As String)
        ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus
    End Sub
    The problem is that this opens a new tab in tabbed browsers. It will quickly become cumbersome to close all those open tabs, so how can I cannibalize an existing tab as well as an existing browser window?

  2. #2
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Open webpage in existing tab of existing browser

    By using the ShellExecute, it will pass the lpszFile name (by associating its type to an existing registry entry), by execution the lpszOp operation. If you set the vbNull to the lpszOp, the devault operation will be executed.

    Because these operations are pre-defined, to open new tabs, it can only be modified in the registry, but its not a great idea to do, because each browser have its own calling scheme, so you could easly ruin up the callings, that is may lead to, you cant use shellexecute on htm files.

    It is may be better to find the browser window (if it is already open), and sending messages to the address bar (its text box), that will point on the url you want to display. And then, you have to execute the browser to start download the page. Its just a thought, but i'm sure this is the only way to re-use an existing tab.

    A side note: if your code cannot find an existing instance of firefox or IE, or opera (that you can use to pass an url to their address bar), you can simply open up a new instance of any browser, by using the above example you shown. So, in either case your program will display the page, but as long as the user using a browser you have hard-coded to be compatible, the existing tabs will be used as well.
    Last edited by Jim Davis; Dec 15th, 2008 at 11:18 PM.

  3. #3

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open webpage in existing tab of existing browser

    Quote Originally Posted by Jim Davis
    It is may be better to find the browser window (if it is already open), and sending messages to the address bar, that will point on the url you want to display. And then, you have to execute the browser to start download the page. Its just a thought, but i'm sure this is the only way to re-use an existing tab.
    Anybody know how to do this? I wouldn't even know how to begin.

    Or is there another alternative?

  4. #4
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Open webpage in existing tab of existing browser

    To find a window, you can use the FindWindow api(). The later thing is a bit like blury to me. But i'm sure its possible to do, you just have to dig into the window classes. A window spy would be a nice tool, to find out what classes and objects are used on each browser. I'm also sure you have to write three different method for the three common browsers.


    Code:
    Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    SetwindowText is may be used to fill up the textbox(address bar), you just have to get its hwnd.

    Edit: Wrong
    MSDN: However, SetWindowText cannot change the text of a control in another application.
    Last edited by Jim Davis; Dec 15th, 2008 at 11:37 PM.

  5. #5

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open webpage in existing tab of existing browser

    I'm thinking this is a fairly common problem, so there should (hopefully) be a simpler solution than all that. I do not have or want any browsers other than IE on my machine, and even if I did I don't view writing code tailored to individual browsers as a particularly elegant solution.

    Any other ideas? Any brainstorming is welcome.

  6. #6

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Open webpage in existing tab of existing browser

    Apparently this can be handled by a browser setting.

    Open links from external programs:
    - In a new browser
    - In a new tab
    - In an existing browser tab

    I'm considering this resolved.

  7. #7
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: [RESOLVED] Open webpage in existing tab of existing browser

    Yeah, the browser have its own settings, to how to handle a page opening, fired by an external source. I just thought you want to implement a feature to your application, that is automatically reuse any existing tab.

    However, in the meantime i found that, by using FindWindow() you can find any instance of the common browsers, by their class name. Also, by using the SendMessage() api, you can send keystrokes, just like Ctrl+L (in firefox) that will set the focuson the address bar, then imitate the keystrokes, to write into the url. Sending the enter key will execute the page downloading. You may also have to set the focus on the window, but im not sure its necessarry to filling up the adressbar.

    I havent completed this, but theoretically it should work. I will look forward to put any working code to the codebank as soon as i get.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Open webpage in existing tab of existing browser

    Open links from external programs:
    - In a new browser
    - In a new tab
    - In an existing browser tab
    i don't believe this will work for firefox as it only has options for an new window or a new tab
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: [RESOLVED] Open webpage in existing tab of existing browser

    There is a quite advanced settings tree, if you type about:config in the address bar.

    http://kb.mozillazine.org/Browser.link.open_external

    This property will manage how to open a link from external calling. Setting the value to 1 should prevent to opening new tabs, but open the page on the current window/tab.

  10. #10

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: [RESOLVED] Open webpage in existing tab of existing browser

    So Firefox cannot ever re-use browser windows? That doesn't sound right.

    EDIT: Thanks, Jim. I knew that didn't sound right.

  11. #11
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: [RESOLVED] Open webpage in existing tab of existing browser

    I'm think this option has been removed in FireFox 2 because of some issues, but it is fixed already. However the option is remained to be invisible, but this way you can set it, i dont see any issues so far.

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