Twitter button failed to load resource file://platform.twitter.com/widgets.js

7.7k Views Asked by At

When copying and pasting the following code for a Twitter button into a text file:

<a href="https://twitter.com/share" class="twitter-share-button" data-via="jpkcambridge">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

I get a javascript error saying 'failed to load resource file://platform.twitter.com/widgets.js'

What could be causing this? Thought I should be able to just copy and paste the code from Twitter.

3

There are 3 best solutions below

0
On BEST ANSWER

You're trying to load a protocol-less URL in a local file. Twitter is serving its script via //platform.twitter.com/widgets.js, which translates to the file: protocol locally. That's the best practice for serving content (it will not generate those annoying mixed content warnings in IE), but just plain won't work in local files.

Try updating that line of the script to https://platform.twitter.com/widgets.js. That should help you test locally; once you're done testing, flip it back to the protocol-less structure.

0
On

Are you sure you've entered the code correctly? Try using a direct link target for your button like this: just change the URL to wherever you want to point it, and the username to whatever username you use. This code is the code from the site:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://anawesomesite.html" data-text="super site of awesome" data-via="Username" data-related="face" data-hashtags="emplosion">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
0
On

Should the src be "https://......" rather than just "//....." ?