Syed Aun Syed Aun - 2 months ago 5
CSS Question

How to call the javascript function of one web page from another web page using onclick attribute in html?

I want to call the javascript function of one web page from another web page. Suppose I have a function

myFunction()
that is defined in
A.html
and I want to use it in
B.html
using
onclick
attribute. How would I call
myFunction()
using
onclick
in
B.html
, so that it could bring some change in
A.html
?

Here is the example...

A.html
includes

<script>
function myFunction() {
document.getElementById("my_contents").style.display="block";
}
</script>


where
B.html
have

<a href="Contents.html#entertainment" target="ccc" onclick="myFunction()">
<h4>Entertainment</h4></a>

Answer

What you're trying to do is impossible because HTML pages don't make "local" functions available to other webpages.

To solve your issue: Create a file called script.js. Place your javascript function inside it without the <script> tags.

In the <head> tag of both your web pages, add <script src="path/to/script.js"></script>

Comments