Seach

Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

dynamic javascript loader

this script aim is to load js files dynamically by passing the location as an array and dynamically include it to dom
load.html
<html>
<head>
<script type="text/javascript" src="load.js" >

</script>
</head>
<body>
<script type="text/javascript" >
options=["module","module1"];//scripts to be loaded
obj.create(options);//calling the creater function
</script>
</body>
</html>
load.js(created using modular design pattern)
var obj=(function()
{

var obj=
{};
this.create=function(script)
{
//script is an array containing the names of the scripts need to be included
for(i=0;i<script.length;i++)
{
js=script[i]+".js";
element=document.createElement('script');//creating dom element script
element.setAttribute('type','text/javascript');//setting script attributes
    element.setAttribute('src',js);
    document.getElementsByTagName('head')[0].appendChild(element);//appending it to head
  }
 
}
return this;

}());

modular javascript approach and event binding

simple adding application using modular javascript and html
<html>
<body>
<input type="text" id="t1">
<input type="text" id="t2">
<input type="button" id="b1" value="Add">
<script type="text/javascript" src="module1.js">

</script>
</body>

</html>
script file
"use strict";
var obj=(function()
{
obj={};
obj.add=function()
{
var n1=parseInt(document.getElementById("t1").value);
var n2=parseInt(document.getElementById("t2").value);
var sum=n1+n2;
alert(sum);
};
return obj;
}());

//document.getElementById("b1").addEventlistner("click",obj.add);
document.getElementById("b1").onclick=obj.add;

how to display file content using ajax php

function select_browser()
{
    //alert("executed");

    var xmlhttp;
    if(window.XMLHttpRequest)
    {
        //alert("browser is internet firefox");
        xmlhttp=new XMLHttpRequest();
        return xmlhttp;
    }
    else
    {
       
        xmlhttp=new ActiveXobject("MICROSOFT.XMLHTTP");
        return xmlhttp;
    }

}

function load_file()
{
   
   
    ajax.onreadystatechange = function()
    {
        if(ajax.readyState == 4 )
        {
            document.getElementById('message').innerHTML=ajax.responseText;
        }
    }
    ajax.open("POST","file.txt");
    ajax.send();
   
}



user exist or not using php ajax

create two php files user_existance_check.php,user validate.php

user_existance_check.php
 <script type="text/javascript">
var obj=null;
if(window.XMLHttpRequest)
{
    obj=new XMLHttpRequest();//used to handle safari,apple,firefox,chrom etc
}
else if(window.ActiveXobject)
{
    obj=new ActiveXobject("Microsoft.XMLHTTP");//internet exploror browser
}
else
{
    throw new Error("Browser Not Support Ajax");
}
function user_check()
{
   
    obj.onreadystatechange=function()
    {
        if(obj.readyState==4 && obj.status==200)
        {
            //alert("completed");
            document.getElementById("message").innerHTML=obj.responseText; //result from the user_validate.php
        }   
    }
    var name_=document.getElementById("name").value;
    obj.open("POST","user_validate.php?name="+name_);//data senderd method is post we can use get also
    obj.send(null);
   
}



</script>
<html>
<body>
<form method="post" name="form1">
<label>Userid</label><input type="text" name="name" id="name" onKeyPress="user_check()"/>//
<label id="message"></label>
</form>
</body>
</html>

user_validate.php

<?php
@mysql_connect("localhost","root","");
@mysql_select_db("test");
$query="select sname from register where sname='".$_REQUEST['name']."'";
$temp_result=@mysql_query($query);
$count=@mysql_num_rows($temp_result);
if($count===0)
{
    echo "user name avaliable";
}
else
{
    echo "user name is not available";
}

?>




how to make a text box only accept numeric values only

protected override void onkeypress(sender s, keypresseventargs e) //overriding onkeypress event method { if ( ( char.isnumber ( e.keychar ) ==fase ) ^ (e.keychar==' \b ' )) e.handled=true; }