Tips And tricks
to get the best from SBI
![]() |
![]() | |
Calling Javascripts From The Support-FilesI see a lot of web pages that have the javascripts in the head section of their pages, rather than in the support-files folder. Why? Well, possibly because it's easier, or the webmaster just doesn't know how to call a script from that folder correctly. So for those of you that DON'T know how to do it, this page should help.There are definite advantages to having your javascripts stored in the support-files folder.....
**************************************************
Do An ExampleI think after you have done this once, then you are going to see how it works, so lets use an actual working example. On the Free Javascripts page, there is a script for listing your links in another window. I have placed this in the head of the page, so you can see what it looks like, and how we are going to get it to work from our support-files folder.So take a look at the source code for that page, and note what I have put in the head section. You will see all the code for the link script. Now, that would be quite inefficient, if I wanted to have that code on EVERY page of my site. So firstly, grab the entire code for the script, and paste it into notepad or some other plain text editor. You will have this. **************************************************
**************************************************
Now save it as a .js file to the support-files folder. Call it what you like, lets say xyz.js.
Next thing is the part that needs to appear on your page, in this case its a button.
important note! The javascript name has to be all lowercase, as SBI will convert capitals to lower case. You may have also noticed the original function was called "showLinks"...I changed it to "showlinks" So if you get a script from the internet, ensure you change any capitalization in the file name.
This isn't meant to be a technical description of how javascripts work, but we need to understand a few things. The "onClick" is whats known as an event handler. In other words, it will trigger an event. In this case, the event is to grab a javascript function called "showlinks", when the button is clicked, and then execute that function. Okay, so that part is fine, but how does the onClick know where the showlinks function is located? **************************************************
One Last Part To AddWe need to add one little line of code to the page, just above the button code will be fine. Those that upload their own HTML can place this in the head section if they prefer.
Note that when the javascript code was in the head of our page, we made no reference to the location of the script. There was no need to, it was on the page already. However, as soon as we moved it to the support-files, we needed to tell the onClick code where the js was. Quite logical really, if you think about it.
**************************************************
A RecapYou find a script that you like on the internet, and want to run it from your support files. Most have two parts. One that goes in the head of your page, and the second part to go in the body of your page.
**************************************************
*Take the head section of the code, copy into notepad, remove the start and end script tags, then save as a .js file using a name
of your choice. This is placed in your support-files folder.*Next place the body section of the script on the page where its needed. *Above this, (or in the head section of your page, depending on where it works best) place the
**************************************************
If you found this, or any other pages to be useful to you, why not let me know? Also, anything else you would like to
see, let me know that too. (for those who want to be pedantic about such things, the script language="javascript" is actually optional, as long as the script src is specified. However, for validation purposes it should be included.) |
||
|
|
||
|
Copyright© SBI-Help 2007-2008
|
||

