1

My first post here :)

Hello everyone,

I have two .htmls, first one is named a.html, and second one is named b.html. I want to go to b.html and call there a function after button on a.html is clicked. The function i would like to run is stored locally in b.html.

Here's the preview of what i'm trying to achieve:

<button type="button" id="rezerwacjawarriors" onclick="location.href='rezerwacja.html' AND CALL FUNCTION ON B.HTML STORED IN B.HTML AFTER LOADING PAGE;">Rezerwacja</button>

Is it even possible to do it like this?

Function i would like to call on b.html make specific div appear.

English is not my native language so im sorry for any misunderstandings and mistakes.

Thanks for help. =)

millyG
  • 11
  • 3
  • https://stackoverflow.com/questions/3842614/how-do-i-call-a-javascript-function-on-page-load Does this help? – Anglesvar Jun 24 '21 at 17:06
  • The thing is i want to run function on b.html automatically only when button on a.html is clicked, not every single time the b.html is loaded. – millyG Jun 24 '21 at 17:13
  • https://stackoverflow.com/questions/40946465/how-to-call-a-function-in-one-html-page-in-another-html-page This might help and welcome to StackOverflow!! – Anglesvar Jun 24 '21 at 17:15
  • @Anglesvar the question you commented is different than this question. The question you commented about is about reusing code, and this question is about calling a function in another page. – programmerRaj Jun 24 '21 at 21:20

1 Answers1

1

You can use a query parameter so that b.html knows to call the function on load.

Page a script

// ... on click
window.location.replace('b.html?callFunction=specialFunction')

Page b script

if (new URLSearchParams(window.location.search).get('callFunction') === 'specialFunction') {
 // Call function or do whatever
}

You can have different actions based on different values of callFunction (or any query string key you choose) Here is a link to a repl which does this.

If you don't like the query parameters at the end of the url, you can remove them - Remove URL parameters without refreshing page.

programmerRaj
  • 1,810
  • 2
  • 9
  • 19