Andrej Tihonov
Andrej Tihonov

Reputation: 669

Let JS execute a function, by loading another html file

I have two html files first.html and second.html. Both are using a file main.js. So lets pretend I am in the second.html and I want to change the window.location.href to first.html. But the same time, I want to call a function, which should be executed in the first.html file just after it is loaded from the second file. So how should I do this. How can I check, from which page the first.html file was called to load?

If I will use the following code,

$(document).ready(function() {...});

The function would be loaded every time, I load this page. But I want to execute the function just, when the page is loaded from the second.html file.

Hope you understand my question.

Upvotes: 0

Views: 70

Answers (2)

Coding Enthusiast
Coding Enthusiast

Reputation: 3933

Okay so you want to load the function only when the first.html is loaded from second.html. You can use localstorage or cookies to store the name of the page.

then use an if statement to check if the cookie is equal let's say to your previous page name then load the function. Then reset the cookie/delete it. Local storage does similar things too but according to w3 schools, they are better than cookies. I don't trust that site much though.

Upvotes: 0

Mavi Domates
Mavi Domates

Reputation: 4511

You can check the url of the page. window.location.href should get you the url, which you can determine which page you're in.

Upvotes: 1

Related Questions