admin管理员组

文章数量:1414614

On the first page:

<button type="button" onclick="myFunction()">click</button>

Then on another page:

myFunction() {
    alert("working");
};

This is the receiving page.

  • all the basic tags
<script>
    function alert() {
        alert("The Link Has Been Successful");
    }
</script>

The question is: How do I call the other function on the other page to alert that page <button onclick="????">?

<body>
    <button onclick="???" type="button">Alert on the other page</button>
</body>

On the first page:

<button type="button" onclick="myFunction()">click</button>

Then on another page:

myFunction() {
    alert("working");
};

This is the receiving page.

  • all the basic tags
<script>
    function alert() {
        alert("The Link Has Been Successful");
    }
</script>

The question is: How do I call the other function on the other page to alert that page <button onclick="????">?

<body>
    <button onclick="???" type="button">Alert on the other page</button>
</body>
Share Improve this question edited Jun 9, 2021 at 12:19 BarryCap 3363 silver badges12 bronze badges asked Nov 10, 2012 at 18:38 LouisdewarLouisdewar 774 silver badges9 bronze badges 6
  • Please explain in more detail what the goal is so it is clear to all exactly what behavior you are looking for. – charlietfl Commented Nov 10, 2012 at 18:42
  • I was trying to activate a function if i was on one page and the function was on another. – Louisdewar Commented Nov 10, 2012 at 18:44
  • 1 what does "function on another page" mean? Concept needs to be explained what you are expecting to happen. If it means do something in other page when that page loads after a specific link is clicked...you need to explain that behavior in more detail. Otherwise it could mean you are trying to do something impossible. We can't read minds – charlietfl Commented Nov 10, 2012 at 18:50
  • Say you had a website hosted and you had these two files in the same folder and if a user pressed a button on one page it activated something like an alert on another. For sake we can call the one we're having the button on 1.html and the alarm 2.html – Louisdewar Commented Nov 10, 2012 at 18:54
  • Apparently it's impossible but maybe you have some way around it even with a different code if you do, do a different code please show it and if you could explain it, thanks. – Louisdewar Commented Nov 10, 2012 at 18:57
 |  Show 1 more ment

3 Answers 3

Reset to default 2

Call a function in one page when the function is on another page

That is not possible. The definition of said function must be present on the current page for it to be executed.


The standard approach is to extract your function into it's own (or global) JavaScript file. Then reference it on pages that need to use it.

You can place all your global functions etc in a .js text file and then on all pages that need to call any of these routines you should declare it like this...

 <script src="myglobals.js" type="text/javascript"></script>

Now you can access your global functions and variables. Good luck!

You do not need to link 2 pages together you just need to link them to your index.html by using: and then js will do all the work for you!

Hope This Helps!

LucaSpeedStack

本文标签: javascriptCall a function in one page when the function is on another pageStack Overflow