How to Call JavaScript Function From URL
The web page typically contains the JavaScript functions inside the specific page source.
There is no way to include Script functions or methods as hyperlinks for our webpage functionality, such as form validations checking for regular expressions, etc. However, we can quickly use bookmarklets to execute our JavaScript functions from our address bar.
Bookmarklets are available in most popular browsers by default; in this article, we will discuss the feature of bookmarklets in popular browsers such as Google Chrome and Mozilla Firefox and how to use that feature to call JavaScript function from URL instead of opening any webpage.
Create Bookmarklets
We can create bookmarklets by bookmarks; it is similar to creating a regularly used bookmark in browsers. The only variation is that we need to write JavaScript code statements rather than any http/s URLs.
Creation of Bookmarks in Google Chrome
Google Chrome provides the most efficient and excellent way to manage bookmarks; we can add or remove our bookmarks by simply following a few quick steps.
-
Open the Chrome browser, then right-click on the bookmarks bar.
-
Click on the Add Page option from the dropdown menu.
-
You can see there is an edit bookmark dialogue.
-
Define the name of your bookmark, then add a JavaScript function in the URL field
-
Select a folder from the bookmarks bar and click save.
-
To test the saved bookmarklet, again right-click on the bookmarks bar. This time select bookmark manager instead of the add page option that we have used already in the first step.
-
In bookmarks manager, you can find your saved folder, open it and view your JavaScript saved URL with name.
-
Once you open it, you can automatically see our given JavaScript code’s execution on a separate web page.
Creation of Bookmarks in Mozilla Firefox
To add, remove, or edit bookmarks, Mozilla Firefox provides a bookmarks menu as well. We need to navigate that menu and manage our bookmarks.
-
Open the Mozilla Firefox browser, right-click on the bookmarks bar, and find the add bookmarks option.
-
Now click on Add bookmark option from the dropdown menu.
-
You can see there is an edit bookmark dialogue.
-
Define the name of your bookmark, then add a JavaScript function in the URL field
-
We can add keywords to the bookmark directly as well; now we need to click save.
-
To test the saved bookmarklet, again right-click on the bookmarks bar. This time select manage bookmarks instead of the add bookmark option. You can see the bookmarks manager dialogue.
-
In the all bookmark section, you can find the bookmarks toolbar; open it.
-
You can easily find your saved JavaScript function by name. Just open it by double-clicking.
-
You can see the execution of our given JavaScript code automatically.
Related Article - JavaScript Function
- JavaScript Return Undefined
- Self-Executing Function in JavaScript
- How to Get Function Name in JavaScript
- JavaScript Optional Function Parameter
- The Meaning of => in JavaScript
- Difference Between JavaScript Inline and Predefined Functions