Link to home
Start Free TrialLog in
Avatar of hapciu
hapciu

asked on

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 ?

thanks
Avatar of gysbert1
gysbert1

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
Avatar of hapciu

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of gysbert1
gysbert1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.