Archive for October, 2008
Moving to previous page in browser using Java Script
Oct 9th
history.go method in Java Script can be used to make the browser go back to previous page in history. This can be invoked using a normal link or on click of a button. The following examples show how to do that.
Go to last page if a link is clicked:
<a href="javascript: history.go(-1)">Back</a>
Go to last page if a button is clicked:
<input type="button" value="Back to Previous Page"
onClick="javascript: history.go(-1)">
How to redirect a page in Java Script
Oct 9th
window.location can set to URL of a page to which redirection is desired. This can be done by placing the Java Script code between head tags. The following example show how a page redirects to http://techpulp.com/newpage.php upon loading. The redirection URL can be changed to any URL.
<head> ... <script language="JavaScript"> window.location="http://techpulp.com/newpage.php"; </script> ... </head>
Reload/Refesh a window in Java Script
Oct 9th
The method window.location.reload() can be used to reload the current browser window in Java Script. This can be invoked on various events like click of a normal link, click of a button etc. The following examples show how to do that.
Reload current window if a link is clicked:
<a href="javascript: window.location.reload()">Reload Window</a>
Reload current window if a button is clicked:
<form method="post"> <input type="button" value="Reload Window" onclick="window.location.reload()"> </form>
How to close a window with a link using JavaScript
Oct 9th
The method self.close can be used to close the current browser window in Java Script. This can be invoked on various events like click of a normal link, click of a button etc. The following examples show how to do that.
Close current window if a link is clicked:
<a href="javascript: self.close()">Close Window</a>
Close current window if a button is clicked:
<form method="post"> <input type="button" value="Close Window" onclick="window.close()"> </form>
How to open a new window on click of a button in Java Script
Oct 9th
In Java Script, window.open method can be used to open link in a new window. This method can used to open a new window when onclick event of the button is fired. The following example shows how a new window can be opened with a URL.
<form>
<input onclick="window.open('http://somesite.com/')"
type="button" value="Open Window" />
</form>
How to rename a table in MySQL
Oct 9th
The RENAME TABLE statement can be used to rename an existing table in MYSQL. The following example shows how name of a table changed from tbl_old to tbl_new.
mysql> RENAME TABLE `tbl_old` TO `tbl_new`; Query OK, 0 rows affected (0.00 sec) mysql>
LIST TABLES statement can be used to view the list of tables present in a database.
How to access command line arguments in a bash script
Oct 9th
The following example script illustrates how command line arguments passed to a bash shell script can be accessed. A special variable $0 contains name of the command and $# contains number of command line arguments passed to the script. Actual arguments can be accessed using the argument number prefixed with $ sign. i.e The first argument is accessed using $1 and $2 to access second argument so on and so forth.
[neo@techpulp ~]# cat cmdargs.sh #!/bin/bash echo Name of script: $0 echo First argument: $1 echo Second argument: $2 echo Number of arguments: $# echo All arguments: $@ [neo@techpulp ~]# [neo@techpulp ~]# sh cmdargs.sh arg1 arg2 More >
Constructor and Destructor for C Program
Oct 8th
constructor and destructor attributes can be used to define a constructor function and a destructor function for a C program. In the following example pg_init function is defined as constructor and this function gets invoked before main() function. The pg_deinit function is defined as destructor and it gets invoked before process exits. Download: condes.c
#include <stdio.h>
static void pg_init () __attribute__ ((constructor));
static void pg_deinit () __attribute__ ((destructor));
static void
pg_init()
{
printf("program init...........\n");
}
static void
pg_deinit()
{
printf("program deinit...........\n");
}
int main()
{
printf("This is main\n");
}
The following is the output.
[neo@techpulp ~]# gcc condes.c -o condes [neo@techpulp ~]# [neo@techpulp ~]# ./condes program init........... This is More >
Get list of interfaces using SIOCGIFCONF ioctl
Oct 8th
The following example shows how the names of all network interfaces can be retrieved using SIOCGIFCONF ioctl. Other ioctls can be used subsequently to retrieve more information like IP address, netmask etc for each interface.
/* ifacelist.c */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
int get_iface_list(struct ifconf *ifconf)
{
int sock, rval;
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
return (-1);
}
if((rval = ioctl(sock, SIOCGIFCONF , (char*) ifconf )) < 0 )
perror("ioctl(SIOGIFCONF)");
close(sock);
return rval;
}
int main()
{
static struct ifreq ifreqs[20];
struct ifconf ifconf; More > 

Recent Comments