Use the 'Today in History' line on my website


#1

Hi there, I would like to use the ‘Today in History’ block which is shown on the main Wikipedia page, on my genealogy website. Is this possible? If so, what code do I use?


#2

See the feed/onthisday API.


#3

Thank you Tgr, but unfortunately I don’t have the knowledge to create this. I thought it could be done with a more simple code/script in my HTML.
Thank you for your effort!


#4

It is pretty simple, e.g.

var date = new Date().toISOString();
$target = $( 'div#target' );
$.get( 'https://en.wikipedia.org/api/rest_v1/feed/onthisday/selected/' + date.substr( 5, 2 ) + '/' + date.substr( 8, 2 ) ).then( function ( data ) {
    $.each( data.selected, function ( _, event ) {
        var $linkedText = $( '<a>' ).text( event.text );
        $.each( event.pages, function( _, page ) {
            if ( page.isTopic ) {
                $linkedText.attr( {
                    title: page.displaytitle,
                    href: page.content_urls.desktop.page
                } );
                return false;
            }
        } );
        $target.append( event.year + ': ',  $linkedText, '<br />' );
    } );
} );

#5

Thank you for that code, Tgr!

But (ahum) … where… do I put it?


#6

Probably something like

<html>
<head>
    ...
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script>
       ...your code here...
    </script>
</head>
<body>
    <div id="target"></div>
    ...your webpage here...
</body>
</html>

but it really depends on what you are using for, what tooling you are using for your site etc.

If you were looking for some kind of preformatted embeddable content, like an iframe, such a thing does not exist (although there are long-term plans to provide it).


#7

Thank you. I’ll try it a.s.a.p.!