Theme Graphic
Theme Graphic

Jonesy's Great Code Archive

This is for me, not you. But I hope you find it useful none-the-less. Suggestions or general comments are appreciated.
Posted on Thursday, November 19, 2009 at 9:15 PM

GreaseMonkey script: Add button to page and bind onclick function


Here is an example of how to add a button to a page at a specific location and then have it call a function onclick.

// Inserts javascript that will be called by the autoCheckOrderButton
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.innerHTML =  'function checkForOrders() { alert("inside checkForOrders"); }';
document.getElementsByTagName("head")[0].appendChild(scriptElement);
window.addButton = function () {
	// Get the location on the page where you want to create the button
	var targetDiv = document.getElementById('offer');
	
	// Create a div to surround the button
	var newDiv = document.createElement('div');
	newDiv.setAttribute('id', 'autoCheckOrder');
	
	// Create the button and set its attributes
	var inputButton = document.createElement('input');
	inputButton.name = 'autoCheckOrderButton';
	inputButton.type = 'button';
	inputButton.value = 'Start Checkin';
	inputButton.setAttribute("onclick", "checkForOrders();");
	
	// Append the button to the div
	newDiv.appendChild(inputButton); 
	targetDiv.appendChild(newDiv);
}
addButton();
Bookmark: Submit To Digg Submit To reddit Submit To del.icio.us Bookmark With StumbleUpon Bookmark With FaceBook Bookmark With Google Bookmarks   Share: Share By Email By Email

2 comments on "GreaseMonkey script: Add button to page and bind onclick function"
Posted by Emanekaf on Tuesday, April 13, 2010 at 12:46 AM
Image Of Author
Awesome
Man, you are a lifesaver. I'm not very experienced with Greasemonkey/Javascript, and I've tried every other method to do this I could find. This is the only method that worked.

Thanks a ton!
Posted by ISHANT on Tuesday, July 20, 2010 at 2:51 AM
Image Of Author
Cool
Man,code is working fine.Button is added on the page but fuction call is not being made,:(

Leave A Comment
Subject:


Comment:
   Bold Italic Underline          Code Link Image Horizontal Rule


Because you do not have or are not logged in to your Programmer's Heaven account, please enter your name.

Name:


To help prevent comment SPAM, please enter the magic code '214' in the box:




Posting Rules
Please follow these rules when posting comments on blog posts.
  • Do not post anything that is racist, hate speech or of a sexual or adult nature.
  • Do not post or link to anything that infringes copyrighted laws.
  • Posting about security or legal topics is fine so long as you are not glorifying or encouraging people to perform illegal activities.
  • Both the author of this blog and the Programmer's Heaven administrators may delete any inappropriate comments without notice at their own discretion.
 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.