1

I'm trying to get a wikipedia article to load onto my site. I'm trying to follow the instructions here: http://en.wikipedia.org/wiki/Wikipedia:WikiProject_Transwiki but I'm at a loss.

I've tried:

var xyz = document.getElementById(url("http://en.wikipedia.org/w/index.php?title=Special:Export&history=1&action=submit&pages=Albert_einstein")

var xyz = $('#xyz').load('http://en.wikipedia.org/w/index.php?title=Special:Export&history=1&action=submit&pages=Albert_einstein');

document.write(xyz);
nalply
  • 26,770
  • 15
  • 78
  • 101
Squirrl
  • 4,909
  • 9
  • 47
  • 85
  • 4
    You cannot without using server side process: http://en.wikipedia.org/wiki/Same_origin_policy – A. Wolff Apr 10 '13 at 20:59
  • The way to do this client-side is simply to use an iFrame. Of course, you won't have any control over its content, but it will work. – Shazboticus S Shazbot Apr 10 '13 at 21:01
  • 2
    @summea only if the external site supports JSONP (note the P) – Kevin B Apr 10 '13 at 21:03
  • Is there a way to do it using Python or another language? They seemed to get it http://stackoverflow.com/questions/4460921/extract-the-first-paragraph-from-a-wikipedia-article-python , no? – Squirrl Apr 10 '13 at 21:17

2 Answers2

1

You can't load content from a different domain than your own through JavaScript. The JS security policy prevents it.

"In computing, the same origin policy is an important security concept for a number of browser-side programming languages, such as JavaScript. The policy permits scripts running on pages originating from the same site – a combination of scheme, hostname, and port number[1 – to access each other's methods and properties with no specific restrictions, but prevents access to most methods and properties across pages on different sites.[1]"
-- Wikipedia, from W3C

Shazbot suggests an iframe, but iframes are deprecated. Use objects :

<div class="timeContainer" style="background:#333; color:#090; padding:10px 0;">
  <div style="text-align:center; width:100%;">Current Date and Time</div> <!-- Heading, replaceable with hx tag -->

<!--[if IE]>
 <object classid="clsid:25336920-03F9-11CF-8FD0-00AA00686F13" data="your.url/" style="width:100%; height:19px;">
  <p>backup content</p>
 </object>
<![endif]-->

<!--[if !IE]> <-->
 <object type="text/html" data="your.url/" style="width:100%; height:19px;">
  <p>backup content</p>
 </object>
<!--> <![endif]-->
</div> <!-- timeContainer -->

Alternatively, you can use cURL (if your server supports it) through PHP. I'm not sure about Python, but I assume python can make use of cURL as well.

Agi Hammerthief
  • 2,114
  • 1
  • 22
  • 38
0

If you are using a modern browser, you should be able to use CORS. According to the Wikipedia API docs, you need to pass an additionnal origin parameter that matches the Origin header sent by your browser.

http://en.wikipedia.org/w/api.php

"origin - When accessing the API using a cross-domain AJAX request (CORS), set this to the originating domain. This must match one of the origins in the Origin: header exactly, so it has to be set to something like http://en.wikipedia.org or https://meta.wikimedia.org . If this parameter does not match the Origin: header, a 403 response will be returned. If this parameter matches the Origin: header and the origin is whitelisted, an Access-Control-Allow-Origin header will be set."

plalx
  • 42,889
  • 6
  • 74
  • 90