Open url in default OS browser using JavaScript - javascript

Is there a way to open URL in default OS browser using JavaScript?
I have button and want to open certain URL whenever user clicks on this button.

window.open('https://example.com', '_blank');
I assume you were looking for a browser solution. Not the OS' default browser but the one you are currntly using this from. Using the OS's default browser is not possible from the browser itself but possible if you were in a dedicated runtime like NodeJS.
If you are looking for the latter one, check out this post on SO: How to use nodejs to open default browser and navigate to a specific URL

Related

Detect supported protocols in browser

I have a web page, where i would like to add links using a custom protocol, to open in my desktop application. For example "myprotocol:www.mytestpage.com".
My issue is, how do I detect, using javascript, if the user has registered my protocol?
If for example I use the link above in firefox, withouth having the protocol registered, I am just taken to an error page. Instead I want to show a dialog telling the user to download my application.
As far as I know you cannot tell whether the protocol has been registered or not.
This answer should give you a step in the right direction. https://stackoverflow.com/a/24129863/7326037

Save PDF when clicked on a download button without showing the OK/Cancel option to save

Is it possible not to show Open/Save dialog? I would like to save the pdf file straight to specific disk location on client PC. Need in NOdeJS
I am automating a LogIn in NodeJS which after a successful login, on clicking a 'Download Report' should download the pdf directly to the computer. On manually testing it does so. However when i run my script after clicking the 'Download Report' it would give a javascript popup for confirmation to save. –
EDIT: After reading your comment again, I noticed you are adding some settings to your profile that sound like they should make it do what you want. Since it isn't working, I think it would be good for you to try what I describe below.
END EDIT
You stated in a comment that you are using Selenium Webdriver to do the login automatically, and the download popup only appears when Selenium is driving the browser. The reason it doesn't keep your settings (to not show the popup) when Selenium is in control, is that Selenium creates a new profile, which has the default settings. You will need to manually create a profile for your browser (I see you are using Firefox), open Firefox with that profile, change the settings to not display the download popup, and tell Selenium to use that profile when it runs.
I'm not sure what your environment is, but I run Selenium from linux, so to create a new profile, I run Firefox from the command line with a -P flag, which opens the profile manager. If you use windows, you may need to look up another way to do this.
Once you have the profile created, and have changed the settings appropriately, change your code to the following:
FirefoxProfile profile = new FirefoxProfile(new File("path/of/firefox/profile"));
Hopefully that will work for you.

How to open a URL in Windows 8 in a specified browser

I've created a JavaScript Windows Store blank app, and added the following code to launch Google in a default browser.
var url = new Windows.Foundation.Uri("http://www.google.com")
Windows.System.Launcher.launchUriAsync(url);
What can I do to specify a specific browser to launch Google in? Is it possible? I think I read somewhere that I can't do this, but I can't seem to find that forum any longer.
You can't do it. Your app doesn't know anything about which browsers are installed, so it won't be able to target a specific one, even if the framework allowed for this (which it doesn't). The other thing to bear in mind is that your user should be in control (in this case by specifying their default browser). You should not try to override this choice.

Can a metro app open the default browser without opening a page?

On button click I want to open the default metro browser (usually IE) without opening a new tab/page, just open the browser, If it is already running then just switch to it as it is to it's current state. Is there a way to do that?
This is just something to try, and may or may not work in your scenario:
If you know the url that is open in the browser, your Windows Store app could launch that same url with the launcher.
In my app, when I launch the same url multiple times, IE will pop up but will not load a second instance of the url.
No. The only way to launch the browser is indirectly through the Launcher. The Launcher requires a Uri or a File and it will open the default program associated with that Uri or file type. If it is http, for example, it will launch the default browser. Unfortunately, it seems that the Uri class does not properly parse the about Uri scheme nor will it accept a blank string or http address without a host.

How do you get a spreadsheet to open Excel instead of a browser window?

If you call javascript window.open and pass a url to a .xls file it open on some machines in the browser window. How can you force it into Excel?
Only the users machine can "force" it into Excel. That said, 99% of the time if you send the correct mime-type and a user has Excel, then it will open in Excel assuming they approve.
And only the server can send the correct mime-type. The document type you pass to a JavaScript window.open call will have no effect on this. In fact, calling window.open will at best just open a superfluous window.
It's best to just link to the document with <a href="foo.xls">. And provided your server is sending a mime-type of application/x-excel or application/x-msexcel this will almost always nudge the browser into opening a new window with the Excel document.
If it's just a static file, and you're using Apache on Linux, check for a file called /etc/mime.types, and ensure that it has the following line in there to associate the .xls file extension with the correct MIME type:
application/vnd.ms-excel xls
I'm guessing the location of that file might vary across systems, but it's in /etc/mime.types on my server which is running RHEL4.
AFAIK you can't do this with JavaScript alone. If you have some sort of scripting language on the server's side you can alter the header to force a download.
Here's a simple tutorial in PHP, but you can easily find one in your favorite language.
You cannot force it into Excel. You can allow the browser to handle it whichever way it is configured to do so, or you can try to force it to download the file and let the user open if from their desktop. To force a download, search for "force download" and your server-side language (PHP, ASP.NET, JSP, etc.)
I don't think you can: you cannot call external programs using Javascript for security reasons. Assuming that the user has Excel installed, you may want to open the new window without the address bar to give the user "the illusion" that the file has been opened with Excel in Internet Explorer.
I wouldn't think this is possible from javascript due to security issues, there would be nothing stopping a rogue webpage from opening dozens of excel/word instances.
Could you not set a hyperlink to the url of the .xls, that way the user would get the usual download prompt to view the file.
Set the http content type to the Excel datatype: application/vnd.ms-excel
You shouldn't need to redirect to a new window, but you will get a popup asking the user to save or open the file.
In relation to (2): I'd worry if a browser could launch an external application and load data into it automatically without user intervention.
This is a setting in each user's browser and not in something that can be set by code. So unfortunately you do not have control of that.
You can not, as it depends on the client machine.
For example on Windows if you want it to always open it with Excel, not in the browser window, you have to open My Computer, Tools, Folder Options, File Types, select the XLS type, and click on Advanced. There are two checkboxes: Browse in same window and Open web documents in place. Uncheck both, close browser window, open it again and try again.
However as I said: it depends on the client, you can not force it.
You can do this using LaunchinIE, an ActiveX Control that will enable HTML pages to start whatever application on the client's machine, without security warnings.
Quote from the site: "At last, web pages can start Word, Excel, or any other corporate application without complaints. Securely."
For this you do have to install the control on the user machine and also add the URL that is allowed to execute local applications to the Windows registry.
Another quote from the site: "To ensure security, LaunchinIE needs to be carefully configured client-side; due to this restriction it's only fit for intranet use."
I use LaunchinIE in our training facility so I can use Internet Explorer as a menu which lets the user choose the machine setup. LaunchinIE then calls a batch script that configures the machine to best support the selected training.
Here are the steps to get this pop-up back when opening a saved Excel file.
Right-Click on the windows [START] button and select Explore to open Windows Explorer window will open.
From the menu select Tools \ Folder Options…
• Choose the File Types tab and scroll down the list of files.
• Left-Click to highlight the XLS Microsoft Excel Worksheet file extension and click on the Advanced button.
In the Edit File Type window, Uncheck the “Browse in same window” option.
Click OK button to accept your changes.
Start a new browser session. The next time you open your Excel spreadsheet in your Inbox, you should be prompted with the following window. Be sure to leave the “Always ask before opening this type of file” as checked. Clicking the Open button should now open your file in Excel.

Categories