Need help in creating external JaVaScript file

Hello All,

First of all please excuse me for my poor/no knowledge of scripting.

I just want to put ShareThis Widget on my site. I know how insert code into a HTML page but I’m having difficulty creating External Java Script File for the same. Here is the code that I need to put to configure this widget:

In header:

<script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript" src="http://s.sharethis.com/loader.js"></script>

Between Body:

<script type="text/javascript">stLight.options({publisher: "ur-6ebd8bc1-82b-5620-20b7-d34984ef50a1"});</script>
<script>
var options={ "publisher": "YOUR PUBLISHER KEY", "position": "left", "ad": { "visible": false, "openDelay": 5, "closeDelay": 0}};
var st_hover_widget = new sharethis.widgets.hoverbuttons(options);

</script> 

I want to create two Ext JS files one for header and another one for the body. What do I need to put in these two files without the script tag. Can anybody please help?

Hi,

It’s not actually necessary to put any of the above code into an external file.
Let me explain:
In the header you are referencing two JavaScript files (buttons.js and loader.js) on a remote server (sharethis.com).
You could of course download these two files, put them somewhere within your website’s folder structure (e.g. /js/) and include them using a relative path.
E.g.:

<script type="text/javascript" src="/js/buttons.js"></script>
<script type="text/javascript" src="/js/loader.js"></script>

However, sharethis.com will probably be operating some kind of CDN , so I wouldn’t change anything here.

Apart from that, in the header you set the variable switchTo5x to be true.
If you want to include this in an external JS file, you would create a plain text file on your computer, copy that line of code into it, save the file somewhere within your website’s folder structure and then include it as above using a relative or an absolute path.
If you are not sure what a relative or absolute path is, this explains it quite well: http://www.boogiejack.com/server_paths.html

In the body you have more of the same.
You could again, create a plain text file on your computer, copy the body code into it, save it somewhere within your website’s folder structure and then include it like this <script type="text/javascript" src="/js/myFile.js"></script>, but I probably wouldn’t bother.
What I would do however, is to move everything into one code block:

<script type="text/javascript">
  stLight.options({publisher: "ur-6ebd8bc1-82b-5620-20b7-d34984ef50a1"});
  var options={ "publisher": "YOUR PUBLISHER KEY", "position": "left", "ad": { "visible": false, "openDelay": 5, "closeDelay": 0}};
  var st_hover_widget = new sharethis.widgets.hoverbuttons(options);
</script>

Whatever you decide, also bear in mind that you shouldn’t change the order in which the JavaScript is executed (i.e. buttons.js should be executed before loader.js, which should be executed before the code at the end of the document).

I hope all of that makes sense. If not, just let me know.

Hello,

Thanks, you’ve explained it so well. I got the whole concept. :slight_smile: