-2

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 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. – epascarello Nov 11 '19 at 16:21
  • 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). –  Nov 11 '19 at 16:30
  • Does this answer your question? [Persist variables between page loads](https://stackoverflow.com/questions/29986657/persist-variables-between-page-loads) –  Nov 11 '19 at 16:31

3 Answers3

1

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

ButchMonkey
  • 1,873
  • 18
  • 30
0

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

?function:1.

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:
    function1()
    break
}

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

Trisma
  • 735
  • 6
  • 19
  • 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.´ – Miguel Leal Nov 11 '19 at 16:41
  • file:///C:/Users/me/Desktop/site/123.html?javascript:alert(%22test%22); This does not work at all. – Miguel Leal Nov 11 '19 at 16:41
  • 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 – Trisma Nov 12 '19 at 12:49
0

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

a.html

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

b.html

<script>
// 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) => {
    console.log(a);
    console.log(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);
        fb(...params);
        break;
    default:
        break
}

// Option 2 (easy solution but try to avoid this):
eval(hash);
</script>