0

sorry this problem might be resolved already, but I have not been able to find the answer.

I basically have 2 files:

form.html code:

<!DOCTYPE html>
<html>
<head>
<script src=".../fileA.js"></script>
<title>BACKUP MONITOR</title>
<script>
    function getEvents()
    {
        alert("getEvents");
    }

    alert("in HTML");
</script>
</head>
<body>
</body>
</html>

fileA.js code:

alert("in fileA");
getEvents();

Question 1:

I can't call getEvents() from fileA.js. Is there any way to do this?

Question 2:

Could I create a new Window from form.html and call getEvents() from there? please see code below:

New form.html code:

<!DOCTYPE html>
<html>
<head>
    <title>BACKUP MONITOR</title>
    <script>
        function getEvents()
        {
            alert("getEvents");
        }

        alert("in HTML");

        var w = window.open();

        var s = w.document.createElement("script");
        s.type = 'text/javascript';
        s.src = ".../fileA.js";
        w.document.body.appendChild(s);

        w.alert("New Window");
    </script>
</head>
<body>
</body>
</html>

thank you very much

3
  • 1
    You need to call it from an onload event since you load the javascript file before you define the getEvents function. Another option is to move the getEvents function above the fileA.js Commented Apr 13, 2014 at 6:13
  • Why not just move your JS from in the document into its own .js file? Then you can link to it easily, modify it simply, and reuse it easier if you need it later in life. Commented Apr 13, 2014 at 6:19
  • thank you NoGray, that worked for me, I have another question now. Can I create a new window from "form.html" add a js to it and call getEvents? I am going to edit my question to give an example Commented Apr 13, 2014 at 8:32

2 Answers 2

1

Change the order in which your scripts load. Your external script .../fileA.js is loading before the getEvents function is defined in your embedded script. The call to getEvents in your external script can't succeed if the function doesn't exist yet.

form.html code:

<!DOCTYPE html>
<html>
<head>
<title>BACKUP MONITOR</title>
<script>
    function getEvents()
    {
        alert("getEvents");
    }

    alert("in HTML");
</script>
<script src=".../fileA.js"></script>
</head>
<body>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Your code should be like this

<!DOCTYPE html>
<html>
<head>
<title>BACKUP MONITOR</title>
<script>
function getEvents()
{
    alert("getEvents");
}

alert("1");
var eventArray = getEventArray();

var w = window.open();
alert("2");
</script>
<script src=".../fileA.js"></script>
</head>
<body>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.