SNIPPET: OPEN URL IN DEFAULT BROWSER

In some cases you would want the URL to open in users default browser, instead of opening itself in UBot’s one, and for example that way replacing the custom HTML that you’ve generated previously.

Bellow is a simple UBot script that will open links clicked in UBot browser in users default browser. JQuery part of the code is written the way that it only applies that behavior to links that have attribute target=”_blank”  set , so other links will still get opened as usual. In case that invalid URL (not starting with “http”) is passed into the command, it will return “” (blank string) without opening the default browser.

SNIPPET & TEST CODE (UBOT AND JQUERY):

 

UPDATE 1:

Added some JavaScript code  which will restore default behavior (opening a new tab) in case user opens the HTML in default browser (calling UBot script from there doesn’t work, that’s why try-catch was added to detect absence of UBot environment).

UPDATE 2:

Replaced “replace” command with “replace regular expression” one and added protection; if URL is still being opened, the script won’t open a new one (sometimes it happened that 2 browser tabs were opened on single click, so this should be solved).

 DEPENDENCIES:

If you like this snippet or have any questions or suggestions, please feel free to leave a comment or subscribe to the RSS feed to have future snippets delivered to your feed reader.

Leave a Reply

Your email address will not be published. Required fields are marked *

*