How to call a function with href to other page on HTML

calling external javascript function from html
how to call external javascript function from html button onclick
how to call javascript function on link click in html
call javascript function from html without event
call javascript function from href with parameters
calling javascript function from anchor tag
js call function from href
call a function on href click jquery

So I have 3 links: 1, 2 and 3 and they all go to the same page and each have a function (1(), 2() and 3()) I want to, when you go to one of those pages from the home page, to go to the 123.html and run 1() or 2() depending on what button they clicked.

I've tried this:

<li><a href="123.html?javascript:1();"><span>Link1</span></a></li>

But it didn't run the function at all. And I want to have all of them like this:

<li><a href="123.html?javascript:1();"><span>Link1</span></a></li>
<li><a href="123.html?javascript:2();"><span>Link2</span></a></li>
<li><a href="123.html?javascript:3();"><span>Link3</span></a></li>

You cannot pass javascript to a page like this unless the page you are going to is looking for the javascript query string in the url.

It would be better to send a query string variable such as ?action=Link1 and then have some javascript check for the "action". You could then run a function based on what the action is.

There are a lot of functions you can find that will do this for you, by looking at the window.location variable

JavaScript function in href vs. onClick, Both onclick & href have different behaviors when calling JavaScript directly. Also the script in href won't get executed if the time difference is� In a function definition, this refers to the "owner" of the function. In the example above, this is the person object that "owns" the fullName function. In other words, this.firstName means the firstName property of this object. Read more about the this keyword at JS this Keyword.

This is not the way of doing. What you could do is add a parameter in your link, something like


You can then extract the parameter on your 123.html page like this :

var url_string = window.location.href
var url = new URL(url_string)
var c = url.searchParams.get("function")

c will have the value of 1, 2 or 3. With that you can make a switch statement and select which function to execute :

switch(c) {
  case 1:

I do not recommend working like this but if you want to do it like that here you go.

JavaScript with HREF : Using JavaScript inside the A Link Tag , HTML is the standard language used to write web pages. A modern day web page (and, by extension, a website) is created by using several different to take a look at the HTML link tag and how you can call a JavaScript function inside it. Of course, most users now access the website and make the phone call from the same device. This opens an opportunity to add a call link – creating an HTML phone number that’s clickable. In this article, we’ll take a look at how to add a call link as well as a few other clickable links such as email and other CTAs.

Based on a similar question you can't (1st answer), but expanding on this answer you can do something like this


<li><a href="b.html#fb('a', 'b');"><span>Link1</span></a></li>


// get hash from url
var hash = window.location.hash;

// clean
hash = hash.replace('#', '');
hash = hash.replace(';', '');
hash = hash.replace(new RegExp("%20", "g"), "");

const fb = (a, b) => {

// Option 1:

 * parse all parameters from string to an array
const parseParams = (str) => {
    let params = str.split(/[/(/)]/)[1].split(',');
    for (i=0; i < params.length; i++) {
        params[i] = params[i].replace(new RegExp("'", "g"), "");
    return params;

// check for existing function - parse only the function name
switch(hash.substring(0, hash.indexOf("("))) {
    case "fb":
        var params = parseParams(hash);

// Option 2 (easy solution but try to avoid this):

Location href Property, An anchor URL - points to an anchor within a page (like location.href="#top"); A new protocol - specifies a different protocol (like location.href="ftp://someftpserver . In this tutorial, we’re going to take a look at the HTML link tag and how you can call a JavaScript function inside it. Though this is a beginner tutorial, you’ll still need to be familiar with the basics of both HTML and JavaScript to understand it.

JavaScript String link() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� The code inside a function is executed when the function is invoked. It is common to use the term "call a function" instead of "invoke a function". It is also common to say "call upon a function", "start a function", or "execute a function". In this tutorial, we will use invoke, because a JavaScript function can be invoked without being called.

JavaScript in HTML, JavaScript in HTML - Learn how to include or embed javascript in your web page, functions defined in the header, which are activated by clicking on the links. Note that each link shows a different way of calling the function, you would only� Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

How can I call an external javascript function in html with a link?, I need to call a javascript function from an external js file. And I want to call it with a link. You can see a part of my code below. HTML EDIT: Add links. Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. w3schools .com THE WORLD'S LARGEST WEB DEVELOPER SITE

  • you can not execute a function on another page. You can add code on that page to look for a querystring, but there is nothing you can do from page A directly.
  • There are lots of ways to get data from page A to page B using just the frontend. However all of them will always be kludges; the proper approach is to learn how to use a proper backend like PHP (or to create an SPA using a suitable framework).
  • Does this answer your question? Persist variables between page loads
  • This looks like it would work, but my javascript is not working, even if I put an alert in the URL, it doesn't show anything.´
  • file:///C:/Users/me/Desktop/site/123.html?javascript:alert(%22test%22); This does not work at all.
  • You should execute the function in your file and not in the url. Instead of the function1() in the switch statement above use your alert
  • Seems a little overcomplicated to run a function :D
  • If you need to add parameters to that function call then you have to do the parsing, which adds this complexity.
  • Yes of course, but he could just parse in function=1 and then in the other file if(function === 1) execute that function, instead of passing it into the url