The following tip allows you to quickly perform calculations via JavaScript in Firefox and Chrome. Many people use Google for this purpose, but this tip even works if your computer is offline.
The basic idea
javascript URLs allow you to perform calculations in a web browser. For example, type the following into the address bar (or click on the link):
javascript:alert(7*45)
With the
keywords feature [1] of Chrome and Firefox, you can abbreviate long URLs, including the one above. In this post, we will create a “calculator” keyword so that the previous calculation can be performed by typing just
js 7*45
You can see that the keyword is invoked by typing the command
js and the argument
7*45, separated by a space. You need to specify the following data to create a keyword:
The following sections show you how to create the calculator keyword in Chrome and Firefox.
Creating the calculator keyword in Google Chrome
Go to “Preferences → Basics → Search → Manage Search Engines...” and create a new entry:
- Name: JavaScript calculator
- Keyword: js
- URL: javascript:alert(%s)
Creating the calculator keyword in Firefox
In Firefox, you can use a shorter URL, because it displays the result of a JavaScript computation in the window. In contrast, Webkit browsers such as Chrome and Safari need a dialog to do that. For example, this URL works in Firefox:
javascript:7*45
To create a keyword in Firefox, you do the following:
- Execute the menu command “Bookmarks → Show All Bookmarks”. Then use the menu with the sprocket symbol to perform “New Bookmark...”.
- Alternatively, “New Bookmark...” is also available via a the context menu (=right-click) of the bookmarks toolbar.
- Enter the data:
- Name: JavaScript calculator
- Location: javascript:%s
- Keyword: js
JavaScript you can use
Related reading
- Browser keywords: using the address bar as a command line
- Math - MDN Docs
- Implementing bookmarklets in JavaScript
7 comments:
As usual in such posts - Opera users community is forgotten.
You could at least mention that in Opera this "js-calc" can be set even as a search engine
I only use Firefox and Chrome and thus have ignored all other browsers, including Internet Explorer and Safari. But I mention that fact in the headline, so there should be little surprise.
You’ll have to decide: You can either be ahead of the masses and use an advanced browser such as Opera or be part of them and find broad acceptance. In either case you lose something and should not complain about it.
What you hint at sounds like a nice feature: Doing calculations can become one of the search engine options in Opera.
small tips but clever solutions to calculate quickly
Why do we have to do all these, when chrome calculates them in the address bar itself. There are extensions also for this, like the one below.
https://chrome.google.com/webstore/detail/odhjfldkefanhaflcloghbcpapedfblh
Several reasons:
- The calculations performed by Chrome in the address bar go to Google by default (you need to be online!).
- The above solution lets you execute arbitrary JavaScript code.
- It works on Firefox, too.
- It’s a hack (as in “fun”). It doesn’t even have to be useful.
Be careful: numbers in JavaScript are 32-bit IEEE 754 binary floating point. Do not rely on the accuracy of non-integer math. For example: 0.1 + 0.2 = 0.30000000000000004. This alone should alert you that javaScript is not as reliable as your desktop calculator.
Sorry, scratch the "32-bit" part. They're larger than that :)
Post a Comment