1

I am developing a mini "ping pong" game with Javascript. The players will use the keyboard of their computers to control the game.

I would like to add an extra feature. I want the user be able to control the game from a smartphone. The user will open a mobile browser and type a url. This mobile page will track the gestures and then will invoke the Javascript function that moves the ping-pong racket.

So, the game runs on PC browser and users will control the racket from their mobile phones browser.

How can i invoke a javascript function that exist in page1.html, from page2.html

3 Answers 3

4

You're going to need some server side logic to handle this. There are ways to make two pages talk to each other, but they need to be inside the same browser window (like frames or iframes). In your scenario, they're on completely different devices. You will need some sort of dynamic server (which can serve PHP/Python/etc. scripts) so that the javascript on the device can report back to the server, and the server can report to the javascript code on the PC.

Sign up to request clarification or add additional context in comments.

3 Comments

I think that, this is the correct approach. Do you have any example or any reference to study? How can i interact with the JS code from PHP?
Here is a basic tutorial for that kind of thing. It refers to jQuery, which is a javascript library that makes syntax a little simpler (there's others like prototype and scriptaculous, but I think jQuery is the most common). Also note that what you're trying to do isn't exactly trivial - you're going to need to identify your users to know what page to send commands to, and then send a large volume of control data through.
You probably should also take latency into consideration - it could take a few seconds for commands to get from the phone to the PC.
0

Could you not include the javascript .js file in the html for both pages? There by being able to call it from both?

i.e. use this on both pages.

<SCRIPT LANGUAGE="JavaScript" SRC="[filename]"></SCRIPT>

Comments

0

i think you want to call the same function from both pages. For this create a .js file and add that function to it. Include the js file in both page and call the function from both pages.

1 Comment

This solution does not fit to my problem.Thx

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.