Go Premium for a chance to win a PS4. Enter to Win


opening a web page

Posted on 2004-09-02
Medium Priority
Last Modified: 2010-04-22
how do i detect the default browser and then load a html page with it ?
i think it's easy to write a script to do this, but i don't know.

anyway, how do i do it in c++ code ? or how do i make a script and then run the script from my c++ prog ?

Question by:hapciu
  • 4
  • 3

Expert Comment

ID: 11987322
Unfortunately, in Linux this is not all that easy since it depends on which desktop environment you're using, if you're even in X windows at all. For example, you could be running the script from a console, where the best effort bet might be to use Lynx. In KDE and Gnome (arguably the two most popular desktop environments), as far as I know the way the default browser is configured is completely different (Linux itself does not have a default central configuration like the Windows registry, which is advantageous in some ways, but also disadvantageous in other ways), so your problem might expand to:
- checking whether or not you are running under X windows (could perhaps just test for the presence of the DISPLAY environment variable with getenv or similar)
- checking which desktop environment is being used
- for gnome I don't know, but in KDE the configs are kept somewhere under ~/.kde/share/ , you could experiment by changing the default browser setting in your KDE control panel to some special string like abc123, and then doing a
grep -ir abc123 .
in the ~/.kde/share directory to see where it is stored. You could probably do something similar for gnome.
Once you've figured out what the default browser is, you'd also have to know how to open a file with that browser, i.e. which command line parameters to use. For example, if using mozilla, you cannot just pass the filename to the browser app on the command line because it will try to open a new mozilla session, which may or may not work depending on whether the user already has mozilla open or not, you need to pass special arguments to tell mozilla to open the page in the existing browser session, similar options exist for konqueror and opera and firefox, although they're mostly smarter about re-using the existing session by default.
Hope some of this helps!
LVL 51

Expert Comment

ID: 11999720
are you talking about "web browsers"?
then you should think about JavaScript code in your HTML page, or use a CGI script which detects the browser by reading the User-Agent header from the HTTP request

Author Comment

ID: 12022492
no, what I am actually talking about is the "run command" dialog.....
in kde, in the start menu there is a "run command" option. if you click it it opens a dialog with a text field... now that dialog is really smart... if you type an executable - it runs it, if you type a webpage - it opens the default browser and loads the page, if you type a music file - it opens the default music player and plays it, etc.

all I want is to write a c++ function that opens a web page .... you give it the address of the webpage as a parameter and it starts the default browser and opens that page.

gysbert1: you may be right, but then again, I hope there is an easier way to acomplish this :)

ps: in windows there is a function called shellExec(...) that does exactly this - default action on a given file. i'm looking for a similar one in Linux

thank you for your answers
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.


Accepted Solution

gysbert1 earned 180 total points
ID: 12023794
hapciu: I don't know offhand whether there is such a function in linux. As I mentioned, linux separates the GUI (Xwindows) from the OS, which has certain disadvantages compared to Windows, at least in terms of graphical integration. FWIW IMHO I think Mac OS X has managed to find a much better balance between good OS and good GUI environment than either Windows or Linux. Windows (still) has a unstable/insecure OS but a pretty good GUI, Linux is excellent in terms of OS stability and security, but lacks a bit of compatibility (drivers) and GUI integration, Mac OS X seems to just shine in all areas.

Anyway, back to the topic at hand. There *might* be a Xwindows API call to do something like shellExec, which *might* be something that hooks into each window manager or desktop environment being used, in which case you might not have to manually detect various things like that. I don't know offhand of such a call though, I would hope that something like that exists though. I have a feeling that it doesn't, the kind of functionality of clicking on certain file types to launch them with certain predefined commands in a special way is something I first started seeing with KDE / Gnome when they first started becoming popular some years ago.

If your program is a GUI app under linux, you could always give it its own config option in a dialog somewhere to specify the browser to use (for a non-GUI app you could still have a config file in a file such as ~/.<yourapp>/<yourapp>.cfg or whatever). Later if you do figure out how to find the default browser in a portable generic way, you could just autopopulate that setting instead of making the user configure it the first time the app is run.

I notice on my environment that there is a variable called WINDOWMANAGER, which for me is set to /usr/X11R6/bin/kde. I'm not sure if this is mandatorily set by Xwindows or something, but you might be able to use that to at least detect what WM the app is running under, and perhaps assume semi intelligent default settings for the user initial browser config dialog, e.g. if in KDE, assume konqueror and populate with the default kde konqueror path (whatever that is), if in gnome assume gecko (or whatever they use), or look to see if mozilla or firefox or something is installed (try shelling 'which mozilla' or 'which firefox' etc. to see if its on the path).

Please let us know if you do find an easier way!
LVL 51

Expert Comment

ID: 12023979
system("/path/to/your/browser URL");

assuming that URL is a parameter somehow, you need to compute the full string for system() first using your args array or whatever.

You also may check exec*() functions, see  man execl

Expert Comment

ID: 12024096
I think hapciu's problem is finding out what the value of /path/to/your/browser needs to be to match the user's default browser setting.
LVL 51

Expert Comment

ID: 12025371
>  I think hapciu's problem is finding out what the value of /path/to/your/browser
difficult in C/C++, either go through all dirs in PATH environment variable and check if it exists their, or use
"which browser" popen() call to get the path back (not very reliable)-:

Expert Comment

ID: 12025473
I suspect the problem is not just the path, but what the actual value of "browser" is, so even "which browser" wouldn't work. I think he wants to know whether "browser" is konqueror, opera, firefox, mozilla, gecko etc. If one knows that, then "which browser" will probably do the trick, in fact it might not even be necessary since doing a "system" should respect the user's path when locating the binary.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month10 days, 3 hours left to enroll

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question