2

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>
BarryCap
  • 326
  • 3
  • 11
Louisdewar
  • 77
  • 4
  • 9
  • Please explain in more detail what the goal is so it is clear to all exactly what behavior you are looking for. – charlietfl Nov 10 '12 at 18:42
  • I was trying to activate a function if i was on one page and the function was on another. – Louisdewar Nov 10 '12 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 Nov 10 '12 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 Nov 10 '12 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 Nov 10 '12 at 18:57
  • you are still being vague... explain all steps of the expected behavior start to finish. From the user clicking button forward. Put details in your question not comments block – charlietfl Nov 10 '12 at 18:57

3 Answers3

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.

Alex
  • 34,899
  • 5
  • 77
  • 90
0

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!

John Citizen
  • 184
  • 1
  • 2
  • 10
  • This is still usefull and i did just have an idea from this if i have this i could have the other page checking if a variable has changed am i able to change a variable from the other page? – Louisdewar Nov 10 '12 at 19:16
  • Well now you're asking for something else... changing a global variable which you CAN do if it has an Id and you use document.getElementById()="newvalue"; etc. – John Citizen Nov 10 '12 at 19:21
  • You can't change a "global" variable from another page. To persist data accross pages use cookies, server state or local storage. – Alex Nov 11 '12 at 13:10
0

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

LucaSpeedStack
  • 513
  • 2
  • 7
  • 21