Skip to content Skip to sidebar Skip to footer

How To Call A Function From Another Html Page?

I want to call a function from another HTML web page I create. If the second Javascript function is this: function g(x){ alert(x); } So I tried this in the first page: functio

Solution 1:

The problem is you are calling testwindow.g before the code in otherWebPage.html has run. You need to wait for the necessary function to be available.

There are several ways to do this, but here is a simple example for how to do this by waiting for the load event.

functionf(){
    var x = prompt("enter");
    testwindow = window.open("otherWebPage.html", "_self");
    testwindow.addEventListener('load', function(){
        testwindow.g(x);
    });
}
f();

Solution 2:

The problem is that

window.open("otherWebPage.html", "_self");

loads "otherWebPage.html" in the current page, which is unloaded.

And an unloaded page can't call functions.

Post a Comment for "How To Call A Function From Another Html Page?"