28 July 2008

Find Mouse Coordinate (position) in JavaScript

function MouseCoords(ev)
{
ev = ev || window.event; // remove browser dependency
if(ev.pageX || ev.pageY)
return {x:ev.pageX, y:ev.pageY};
else if (ev.clientX || ev.clientY)
{
var posx = ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
var posy = ev.clientY + document.body.scrollTop + document.documentElement.scrollTop;
return { x:posx , y:posy };
}
return { x:-1 , y:-1 }; // invalid/unknown mouse event
}


Note: This function does not work for mouse scrolling in Mozilla FireFox 2.0 or previous versions.

Detect Right Button Click in JavaScript

function IsRightButtonClicked(e) // e : mouse event
{
var rightclick = false;
e = e || window.event;
if (e.which)
rightclick = (e.which == 3);
else if (e.button)
rightclick = (e.button == 2);
return rightclick;
}

Disable Default Right Click Menu of Browsers

Observe the attribute oncontextmenu="return false;". It causes the default right click popup menu to be disabled. It can be used on body tag too.

<html>
<
head>
<
title>Disable Default Right Click Menutitle>
head>
<
body>
Right click on the following images to observe the effect.<br />
<img src="http://lh5.ggpht.com/alorChhota/R8qXNVDlYMI/AAAAAAAAATA/YS_ab1pnwII/s144/SDSL%20Picnic007.jpg" oncontextmenu="return false;" />
<img src="http://lh5.ggpht.com/alorChhota/R8qXNVDlYMI/AAAAAAAAATA/YS_ab1pnwII/s144/SDSL%20Picnic007.jpg" />
div>
form>

body>

html>

Note: In Opera, default right click popup menu can’t be disabled.

Validation from JavaScript before Form Submit

Look at the attribute of form tag onsubmit="return validate();".

<html>
<
head>
<
title>Validate before submittitle>
<script language=javascript>
function validate()
{
var v = document.getElementById("SearchString").value;
if(v.length == 0)
{
alert("No input given");
return false;
}
return true;
}

script>
head>

<body>

<form name="myform" action="http://www.google.com/search" method="GET" onsubmit="return validate();">

Search String: <input type="text" id="SearchString" size="25" value="" name="q">
<br> <input type="submit" value="Search in Google"><br>
div>
form>
body>
html>

24 July 2008

Browser Detection in JavaScript

The greatest challenge in web programming, in my opinion, is to make your website compatible to all browsers. [I say - A new browser or even a new version of an old browser is a new pain to programmer.]. Whatever, browser detection is a very important task for web programmer. Following is the JavaScript code to detect browser and OS.

var BrowserDetect = {

init: function ()

{

this.browser = this.searchString(this.dataBrowser) || "An unknown browser";

this.version = this.searchVersion(navigator.userAgent)

|| this.searchVersion(navigator.appVersion)

|| "an unknown version";

this.OS = this.searchString(this.dataOS) || "an unknown OS";

},

searchString: function (data)

{

for (var i=0;i {

var dataString = data[i].string;

var dataProp = data[i].prop;

this.versionSearchString = data[i].versionSearch || data[i].identity;

if (dataString) {

if (dataString.indexOf(data[i].subString) != -1)

return data[i].identity;

}

else if (dataProp)

return data[i].identity;

}

},

searchVersion: function (dataString)

{

var index = dataString.indexOf(this.versionSearchString);

if (index == -1) return;

return parseFloat(dataString.substring(index+this.versionSearchString.length+1));

},

dataBrowser: [

{ string: navigator.userAgent,

subString: "OmniWeb",

versionSearch: "OmniWeb/",

identity: "OmniWeb"

},

{

string: navigator.vendor,

subString: "Apple",

identity: "Safari"

},

{

prop: window.opera,

identity: "Opera"

},

{

string: navigator.vendor,

subString: "iCab",

identity: "iCab"

},

{

string: navigator.vendor,

subString: "KDE",

identity: "Konqueror"

},

{

string: navigator.userAgent,

subString: "Firefox",

identity: "Firefox"

},

{

string: navigator.vendor,

subString: "Camino",

identity: "Camino"

},

{ // for newer Netscapes (6+)

string: navigator.userAgent,

subString: "Netscape",

identity: "Netscape"

},

{

string: navigator.userAgent,

subString: "MSIE",

identity: "Explorer",

versionSearch: "MSIE"

},

{

string: navigator.userAgent,

subString: "Gecko",

identity: "Mozilla",

versionSearch: "rv"

},

{ // for older Netscapes (4-)

string: navigator.userAgent,

subString: "Mozilla",

identity: "Netscape",

versionSearch: "Mozilla"

}

],

dataOS : [

{

string: navigator.platform,

subString: "Win",

identity: "Windows"

},

{

string: navigator.platform,

subString: "Mac",

identity: "Mac"

},

{

string: navigator.platform,

subString: "Linux",

identity: "Linux"

}

]

};

BrowserDetect.init();

Now BrowserDetect.browser, BrowserDetect.version & BrowserDetect.OS will contain the browser name, browser version and OS name respectively.

23 July 2008

Registry Key : Add & Read in VS 2005

Shimul, my friend, suggested me to add some advanced feature of creating setup project like registry key. This blog contains basic steps of adding a registry key from setup project as well as reading a registry key value from the program.

How to see the registry keys:

Click on Start Menu > Run > regedit [enter]. Here you will see the following.

You can find a short description on registry key here http://www.dotnetheaven.com/Uploadfile/sushmita_kumari/RegistryKeys102212006232904PM/RegistryKeys1.aspx

How to Add Registry Keys from Setup Project:

  1. In Solution Explorer, right click on Setup Project.


  2. Click on View > Registry.
  3. Expand the tree view of Registry key (setup) in the left side where you want to add a key. Here we will add a key in following directory.


  4. Right click on [Manufacturer] > New > String Value.
  5. Rename the newly added key name. Set its value from it’s properties.

How to modify Manufacturer:

  1. Go to properties of Setup Project.
  2. Change the field Manufacturer

You can see the Registry value after installing which is like below:



Sample Code to Read Registry Key:

using Microsoft.Win32;

string v = getRegistryKeyValue(@"SOFTWARE\BUET", "MyKey");

MessageBox.Show("Key value : " + v);

private string getRegistryKeyValue(string keyPath, string keyName)

{

try

{

RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyPath);

string s = (string)(rk.GetValue(keyName));

rk.Close();

return s;

}

catch (Exception ex)

{

return null;

}

}

21 July 2008

Create Setup Files in .NET

Most of the time, we, the programmers, want to create the Setup File for our projects. This blog contains the basic steps of creating setup files in .NET. The easy steps are as follows (in MS Visual Studio 2005)-

  1. Create a Setup Project. [File > New > Project]
  2. Add your Project to Setup Project.
    1. Open Solution Explorer window
    2. Right click on Solution Icon
    3. Add Exsiting Project (Here JSAPIObfuscatorProg)
  3. Create Exe File
    1. Right click on Application Folder > Add > Project Output
    2. Select Primary Output of the project
  4. Select Location of Installed Components
    1. Go to Properties of Application Folder
    2. Change Default Location (if you wish)
    3. Make Always Create = true
  5. Add Shortcut to Program Files
    1. Go to properties of User’s Programs Menu
    2. Make Always Create = true
    3. Click on User’s Program Menu
    4. Right Click on the blank white space

    5. Select Create New Shortcut
    6. Choose the target item [Application Folder > Primary Output From…]

  6. Add Shortcut to User’s Desktop
    1. Similar to Program File shortcut
  7. Change Icon of Exe file
    1. Right Click on YOUR PROGRAM [Here JSAPIObfuscatorProg]

    2. Select Properties
    3. Select Icon file

  8. Change Name & Icon of Shortcuts
    1. Go to properties of created shortcut from step 5
    2. Change Name
    3. Select Icon : You might have to add an icon to Application Folder.
      i.
      Right Click on Application Folder ii. Add an *.ico file.
  1. Prerequisites: Sometimes you might need to install .NET framework, Crystal Report etc. before installing your product. In this case, you need to add Pre-requisites. If prerequisites will be installed only if they are not installed in user computer.
    1. Go to Project -> Properties.

    2. Click on Prerequisites button

    3. Select the prerequisites from the list
    4. Choose Download prerequisites from same location as my application option

  2. Build the project. You will get the setup files insides Debug folder.

 

© 2007 t!ps n tr!cks: July 2008



Template unik dari rohman


---[[ Skip to top ]]---