0

I want to make a little web page which calculates stuff, based on various data from another site. Let's say for an online browser game.

I would like to have it take some specific details from the web pages on the game, but I don't even know where to start.

Is this doable with javascript (if not I would like to know with what language it is)? Could anyone give me a general of guideline of how this can be done?

frrlod
  • 6,475
  • 8
  • 31
  • 37
  • What kind of data are you going to be accessing and from where? – Lee Taylor Apr 09 '13 at 17:52
  • 1
    First I would learn about the [Same Origin Policy](https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript) – epascarello Apr 09 '13 at 17:53
  • @LeeTaylor, something along the lines of copy-pasting some game-information to make calculations automatically. – frrlod Apr 09 '13 at 17:58
  • Javascript can do this, but only if the data from the other site is formatted correctly (jsonp). This seems like quite a vague question. Can you give any other details on what exactly you are trying to do and what site you want data from? – Nate Apr 09 '13 at 17:58

2 Answers2

0

You can use a combination of AJAX and XMLHTTPRequest, but these are subject to the same origin policy.

Of course, it would make it a lot easier if the external site can send back data in JavaScript Object Notation format (JSON) for readability.

There is a similar question asked here for how to obtain the contents of an external site using XMLHTTPRequest here: How to specify an external website for XMLHTTPRequest

There is another similar question on using XMLHTTPRequest and JSON: Cross-domain JSON request?

Community
  • 1
  • 1
KernelPanik
  • 8,169
  • 1
  • 16
  • 14
0

I have a standard function for using AJAX to get information dynamically. I use PHP as my listener. Your listener would have to be able to accept variables from the URL, like PHP $_GET[].

In the example below, your listener named "source_url.php" would have to check the values received in $_GET[field] and then simply print/echo the result.

JAVASCRIPT:

function get_(url, func)
{
 var http;
 try { http = new XMLHttpRequest(); } catch (e) { try { http = new ActiveXObject(\"Msxml2.XMLHTTP\"); } catch (e) { try { http = new ActiveXObject(\"Microsoft.XMLHTTP\"); } catch (e) { alert(\"Your browser broke!\"); return false; } } }

 http.open(\"GET\", url, true);
 http.onreadystatechange = function() { if(http.readyState == 4) { func(http); } }
 http.send(null);
}

function get_info(fieldname)
{
 get_("source_url.php?field=" + fieldname, showResult)
}

function showResult(h)
{
 alert("The result is: " + h.responseText);
}

HTML

<button onClick='get_info("name");'>Get the ship Name</button>
<button onClick='get_info("reg");'>Get the Registration Number</button>
<button onClick='get_info("capt");'>Who is the Captain?</button>

PHP

<?php
 if ($_GET[field] == "name") { print "U.S.S. Enterprise"; }
 if ($_GET[field] == "reg") { print "NCC - 1701"; }
 if ($_GET[field] == "capt") { print "Jean Luc Picard"; }
?>

I use this all the time, though I have created a more advanced version with authentication security. This is where you should start if you are just learning how AJAX works.