Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

opening a web page

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 ?

  • 4
  • 3
1 Solution
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!
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
hapciuAuthor Commented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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!
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
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.
>  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)-:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now