If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Can Javascript Read Source Code?

Hi! I'm new to Javascript and was wondering if it can read a website's source code. What I'm trying to accomplish is taking information from btc-e.com about bitcoin prices and trading information to make a calculator to help me with trading. If Javascript cannot read and copy parts of a websites source code can anyone think of a way to do this?

What code would I need to communicate with the javascript of a different website?

If you mean where both scripts are attached to pages displayed in the browser at the same time then you can use postMessage in JavaScript on the web one page and attach a message event listener in the javaScript of the other web page to pass information from one to the other.

It's a two-way communication. Both websites that need to communicate must have the postMessage() and 'message' event handler to enable them to exchange messages. And they need to have agreement what information they can share.

So if the target website doesn't have those or, if they have, but only allow communication between their domain and/or trusted sites, or they only allow certain information to be shared, then you won't be able to get the information you need from them.

Despite the help you have given me, I'm still lost. Can you write the code for me that I would need for getting the 9th line after the semi colon from the source code on this site litecoinexchangerate.org
Remember I'm really new to JavaScript and coding in general. Your help is greatly appreciated.

You don't have direct access to the source code of the page - only the document object model view of it. You access the source code by referencing tags within the page - there are no lines in HTML code so there is no 9th line to retrieve.

You can solve this with a server-side solution (e.g. using cURL in PHP) just like how Facebook extracts the document title and description of a website when you share a link. But still, this is not reliable in your case as the HTML source of the target site may change in the future so the information you need from the HTML output might have been changed, moved or deleted.

YQL is also your alternative as xelawho suggested. (In fact, I want to use it in a new mobile app that I plan to develop )

But note that using it is subject to usage limits and restrictions. From their FAQ:

How many times can I call YQL in a minute/hour/day?
Rate limits in YQL are based on your authentication. If you use IP-based authentication, then you are limited to 2,000 calls/hour/IP to the public YQL Web service URL (/v1/public/*) or 20,000 calls/hour/IP to the private YQL Web service URL (/v1/yql/*) that requires OAuth authorization. See the YQL Web Service URLs for the public and private URLs. Applications (identified by an Access Key) are limited to 100,000 calls/day/key*. However, in order to make sure the service is available for everyone we ask that you don't call YQL more than 0.2 times/second or 1,000 times/hour for IP authenticated users and 2.7 times/second or 10,000 times/hour