Best Practice for Javascript API Building

Posted on 2010-04-06
Medium Priority
Last Modified: 2013-11-08
Looking at many services they are putting javascript files on your page which ends up generating the content in the form of forms, linkbacks, buttons, ads, ect.

To keep it simple lets say I wanted to create a button that users could click on any site and it would post back to a database on my main site and tell the DB what page they clicked. Kind of like dig.

I'm assuming most use JSON or similar to post the data back. However, how should the JS file be constructed that would generate the button on the site?

I haven't found any best practices or best ways to go about doing this.

Best Regards.
Question by:TechTremor
  • 3
LVL 83

Expert Comment

ID: 29948752
Hello TechTremor,

Not sure I understand your need but have a look to this artice : http://wiki.apache.org/struts/AjaxStruts


Author Comment

ID: 29949839
That explains a bit of the Ajax side, let me try to explain a bit better.

I would like to write some javascript that i could be included on any page on the web via:

<script language="javascript" src="site.com/jsfile.js">

This script would generate a button that could be clicked to send back URL destination to the host site (in this case site.com) server.

My question is whats the best practice to get the jsfile.js to generate a usable button on any site the js file is put on.

<script language="javascript" src="http://www.antonspaans.com/mysmugmug.js">

Open in new window

LVL 83

Accepted Solution

leakim971 earned 2000 total points
ID: 29952715
something like :

window.onload = function() {
	button = document.createElement("INPUT");
	button.type = "button";
	button.setAttribute("onclick", "document.location.href = 'http://www.experts-exchange.com'");
	button.value = "Go to EE";
	document.getElementsByTagName("body")[0].appendChild( button );

Open in new window

LVL 83

Expert Comment

ID: 30223457
Thanks for the points!

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question