Pages

Showing posts with label Jquery Samples. Show all posts
Showing posts with label Jquery Samples. Show all posts

Monday, March 11, 2013

Simple jquery popup window opens when page load

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple JQuery Modal Window from Queness</title>
<script type="text/javascript"

src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>

<script type="text/javascript">
$(document).ready(function() {

var id = '#dialog';

//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});

//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow",0.8);

//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
             
//Set the popup window to center
$(id).css('top',  winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);

//transition effect
$(id).fadeIn(2000); 

//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();

$('#mask').hide();
$('.window').hide();
});

//if mask is clicked
$('#mask').click(function () {
$(this).hide();
$('.window').hide();
});

});

</script>

<style type="text/css">
body {
font-family:verdana;
font-size:15px;
}

a {color:#333; text-decoration:none}
a:hover {color:#ccc; text-decoration:none}

#mask {
  position:absolute;
  left:0;
  top:0;
  z-index:9000;
  background-color:#000;
  display:none;

#boxes .window {
  position:absolute;
  left:0;
  top:0;
  width:440px;
  height:200px;
  display:none;
  z-index:9999;
  padding:20px;
}
#boxes #dialog {
  width:375px;
  height:203px;
  padding:10px;
  background-color:#ffffff;
}
</style>
</head><body>
<h2><a href="http://www.queness.com/">Simple jQuery Modal Window Examples from

Queness WebBlog</a></h2>
<div style="font-size: 10px; color: rgb(204, 204, 204);">Except where otherwise

noted, content on this site is licensed under a Creative Commons Attribution 3.0

License.</div>

<div id="boxes">
<div style="top: 199.5px; left: 551.5px; display: none;" id="dialog"

class="window">
Simple Modal Window |
<a href="#" class="close">Close it</a>
</div>
<!-- Mask to cover the whole screen -->
<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;"

id="mask"></div>
</div>
</body>
</html>

Friday, January 18, 2013

Virtual keyboard sample using javascript


<html>
<head runat="server">
    <title></title>
    <script type="text/javascript" src="vkboard.js"></script>
    <script><!--

        // This example shows the very basic installation
        // of the Virtual Keyboard.
        //
        // 'keyb_change' and 'keyb_callback' functions
        // do all the job here.

        var opened = false, vkb = null, text = null;

        function keyb_change() {
            document.getElementById("switch").innerHTML = (opened ? "Show keyboard" : "Hide keyboard");
            opened = !opened;

            if (opened && !vkb) {
                // Note: all parameters, starting with 3rd, in the following
                // expression are equal to the default parameters for the
                // VKeyboard object. The only exception is 15th parameter
                // (flash switch), which is false by default.

                vkb = new VKeyboard("keyboard",    // container's id
                           keyb_callback, // reference to the callback function
                           true,          // create the arrow keys or not? (this and the following params are optional)
                           true,          // create up and down arrow keys?
                           false,         // reserved
                           true,          // create the numpad or not?
                           "",            // font name ("" == system default)
                           "14px",        // font size in px
                           "#000",        // font color
                           "#F00",        // font color for the dead keys
                           "#FFF",        // keyboard base background color
                           "#FFF",        // keys' background color
                           "#DDD",        // background color of switched/selected item
                           "#777",        // border color
                           "#CCC",        // border/font color of "inactive" key (key with no value/disabled)
                           "#FFF",        // background color of "inactive" key (key with no value/disabled)
                           "#F77",        // border color of the language selector's cell
                           true,          // show key flash on click? (false by default)
                           "#CC3300",     // font color during flash
                           "#FF9966",     // key background color during flash
                           "#CC3300",     // key border color during flash
                           false,         // embed VKeyboard into the page?
                           true,          // use 1-pixel gap between the keys?
                           0);            // index(0-based) of the initial layout
            }
            else
                vkb.Show(opened);

            text = document.getElementById("textfield");
            text.focus();

            if (document.attachEvent)
                text.attachEvent("onblur", backFocus);
        }

        function backFocus() {
            if (opened) {
                var l = text.value.length;

                setRange(text, l, l);

                text.focus();
            }
        }

        // Callback function:
        function keyb_callback(ch) {
            var val = text.value;

            switch (ch) {
                case "BackSpace":
                    var min = (val.charCodeAt(val.length - 1) == 10) ? 2 : 1;
                    text.value = val.substr(0, val.length - min);
                    break;

                case "Enter":
                    text.value += "\n";
                    break;

                default:
                    text.value += ch;
            }
        }

        function setRange(ctrl, start, end) {
            if (ctrl.setSelectionRange) // Standard way (Mozilla, Opera, ...)
            {
                ctrl.setSelectionRange(start, end);
            }
            else // MS IE
            {
                var range;

                try {
                    range = ctrl.createTextRange();
                }
                catch (e) {
                    try {
                        range = document.body.createTextRange();
                        range.moveToElementText(ctrl);
                    }
                    catch (e) {
                        range = null;
                    }
                }

                if (!range) return;

                range.collapse(true);
                range.moveStart("character", start);
                range.moveEnd("character", end - start);
                range.select();
            }
        }

 //--></script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <table border="0" width="60%">
            <tr>
                <td width="100px">
                    <textarea id="textfield" rows="12" cols="50"></textarea>
                </td>
                <td width="10px">
                </td>
            </tr>
        </table>
        <p>
            <a href="javascript:keyb_change()" onclick="javascript:blur()" id="switch" style="font-family: Tahoma;
                font-size: 14px; text-decoration: none; border-bottom: 1px dashed #0000F0; color: #0000F0">
                Show keyboard</a></p>
        <div id="keyboard">
        </div>
    </div>
    </form>
</body>
</html>

Friday, December 14, 2012

Javascript code to redirect mobile phone users


I have a javascript code to redirect mobile phone users. Could you please check the code, I've picked it up from the net, total newb and I'd value your input regarding the quality of the script... Does it cover all types of mobile phones?


<script type="text/javascript">
function RedirectSmartphone(url){
    if (url && url.length > 0 && IsSmartphone())
    window.location = url;
}
function IsSmartphone(){
    if (DetectUagent("android")) return true;
    else if (DetectUagent("iphone")) return true;
    else if (DetectUagent("ipod")) return true;
    else if (DetectUagent("symbian")) return true;
    return false;
}
function DetectUagent(name){
    var uagent = navigator.userAgent.toLowerCase();
    if (uagent.search(name) > -1)
    return true;
    else
    return false;
}
RedirectSmartphone("http://mobile.version.com");
</script>
   
Ty very much!   

Thursday, March 8, 2012

Redirect your website to a mobile site version through JavaScript

SCENARIO :
The user needs to be redirected to the mobile version of the site (home page) if it’s trying to access the site from a mobile device.

SOLUTION:

UPDATE 25/07/2011 : Version 0.9.5 released with support for “tablet_url”, “keep_path” and “keep_query” properties. Ipad and other tablet devices have been excluded from the list of mobile devices by default. You can use “tablet_redirection” and “tablet_url” parameters for tablets.

To solve this problem, the best approach is implementing something server-side, and I find a good approach using the WURFL file to check the capabilities and features of mobile devices. Read here to know more about WURFL.

Sometimes, a server-side solution can become difficult to implement especially if we have a CDN or reverse proxy (sitting in front of our Web Server) caching our pages.

Here JavaScript comes to the rescue and I wrote a script that makes the redirection happen called “redirection_mobile.js“.

You can find the source here on Github.

The first thing to keep in mind is that the function implemented checks the User-Agent string from the Navigator object and from there it decides if the redirection needs to happen.

In some cases the user wants to access to the Desktop version of the site from a mobile device (sometimes the desktop version has more functionality). The script handles this situation as well, it checks if the previous page hit was one from the mobile site (we can suppose the user clicked on a link such “Go to full site“) or if there is a specific parameter in the querystring of the URL. In those cases the redirection won’t occur. To keep the user in the desktop version for the whole session, sessionStorage object has been used, specifically an item will be stored to distinguish if we’re browsing through the desktop site.

There is a fallback for old browsers that don’t support sessionStorage, and a cookie will be used. The cookie that makes the access to the desktop version from a mobile device possible will expiry in one hour or you configure the expiry time.

iPhone, iPad, iPod, Android phones support completely sessionStorage, there are still some versions of Blackberry that using IE don’t and so we still need the “cookie” fallback.

The function accepts an argument which is a configuration object with few properties:

- mobile_prefix : prefix appended to the hostname, such as “m” to redirect to “m.domain.com”. “m” is the default value if the property is not specified.

- mobile_url : mobile url to use for the redirection (without the protocol), such as “whatever.com”/example to redirect to “whatever.com/example”. If “mobile_prefix” is existing as well, “mobile_prefix” will be ignored. Empty string is the default value.

- mobile_scheme : url scheme (http/https) of the mobile site domain, such as “https” to redirect to “https://m.domain.com”. The protocol of the current page is the default value.


- noredirection_param – up to version 0.6 param was used: parameter to pass in the querystring of the URL to avoid the redirection (the value must be equal to “true”). Default value is “noredirection”. Eg: http://domain.com?noredirection=true. It’s also the name of the item in the localStorage (or cookie name) used to avoid mobile redirection. Prior version 0.9.5 this parameter was called “redirection_paramName”, but I renamed it to make the meaning clearer.

- cookie_hours : number of hours the cookie needs to exist after redirection to desktop site. “1″ is the default value.

- tablet_redirection : boolean value that enables/disables(default) the redirection for tablet such as iPad, Samsung Galaxy Tab, Kindle or Motorola Xoom. – Default:false. The value needs to be a string (so wrapped in double or single quotes). If ‘tablet_url’ parameter not specified, the user will be redirected to the same URL as for mobile devices.

- tablet_url : url to use for the redirection in case the user is using a tablet to access the site. Default value is “”
- keep_path : boolean to determine if the destination url needs to keep the path from the original url. Default value is ‘false’

- keep_query : boolean to determine if the destination url needs to keep the querystring from the original url. Default value is ‘false’

- beforeredirection_callback : if specified, callback launched before the redirection happens. If a falsy value is returned from the callback the redirection doesn’t happen.

To use “redirection_mobile” function, you need to load your script in the HTML of the “desktop” pages and call it as SA.redirection_mobile(config). See the code below:


<!doctype html>
<html>
    <head>
        <title></title>
        <script type="text/javascript" src="/js/redirection_mobile.min.js"/>
        <script type="text/javascript">
            SA.redirection_mobile ({noredirection_param:"noredirection", mobile_prefix : "mobile", cookie_hours : "2" });
        </script>

For instance, in this case, accessing from a mobile device to http://www.domain.com, you’ll be redirected to “http://mobile.domain.com“.

Considering the previous code, from version 0.6, if you hit a page such as “http://domain.com/?noredirection=true” the redirection won’t happen. For all the browser session, if sessionStorage is supported by the browser, the redirection won’t occur. If sessionStorage (HTML5) is not supported, a cookie “noredirection=true” will be stored for 2 hours and it will block the redirection to the mobile site.
If sessionStorage (HTML5) is not supported, a cookie named “noredirection” will be stored for 2 hours and it will block the redirection to the mobile site.

The script from version 0.5 allows you to redirect the user to whatever url. Thus if you need to redirect the user to “https://domain2.com/mobile” now you can invoke the function like this:


    <script type="text/javascript">
        SA.redirection_mobile ({mobile_scheme:"https", mobile_url : "domain2.com/mobile"});
    </script>



Alternatively you can use “redirection_mobile_self.js”, that is it’s an anonyimous self-executing function and it uses it uses the default values for the different properties:

- “mobile_prefix” : “m”
- “redirection_paramName” : “mobile_redirect”
- “cookie_hours” : 1
- “mobile_url” : “”
- “mobile_scheme” : protocol of the current page
- “tablet_redirection” : false
- “beforeredirection_callback” : n/a

It doesn’t need any configuration or any invocation, so you just need to drop it on your webserver and call the script from the HTML of the “desktop” pages . See code below:

<!doctype html>
<html>
    <head>
        <title></title>
        <script type="text/javascript" src="/js/redirection_mobile_self.min.js"/>

in this case, accessing from a mobile device to http://www.domain.com, you’ll be redirected to “http://m.domain.com“.

To redirect to a desktop/standard version of the site from a mobile device, you may need to embed a link in your mobile pages such as

<a href="http://www.domain.com">Go to main site</a>

and the script included in the desktop page will do the rest.

I also created “redirection_mobile_testable.js” that is just a copy from “redirection_mobile.js”, but it’s using few arguments such as “document”, “window”, “navigator” for testing purpose. Test cases have been written, using QUnit, to test this script and they mock “document”, “window” and “navigator” in a rudimentary way.

The scripts have their minified versions (used YUI compressor).

If you want to test the script on different devises within your desktop browser, you can use a plugin for Firefox called User Agent Switcher, that you can download here.

Feel free to fork the project and improve it if necessary.

..and feel free to make a donation from this page 

UPDATE 20/12/2010 : Added support for more devices and fixed a critical issue on IE

UPDATE 05/01/2011 : Version 0.5 released with support for “mobile_url” and “mobile_scheme” properties

UPDATE 02/04/2011 : Version 0.8 released with support for “ipad_redirection” and
“beforeredirection_callback” properties

You can support me clicking the DONATE button you can find on my site http://www.sebastianoarmelibattana.com/projects/js-redirection

Monday, July 18, 2011

Jquery Validation with Regular Expressions

This time I want to explain about "Form validation using regular expressions with jquery". I had developed a tutorial using jquery.validate plugin, It's very simple. Implement this and enrich your web projects. Take a look at live demo

Jquery Validation with Regular Expressions.

Download Script     Live Demo

 Placed in header tag
==========================================
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js
"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" >
$(document).ready(function() {
$.validator.addMethod("email", function(value, element)
{
return this.optional(element) || /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/i.test(value);
}, "Please enter a valid email address.");

$.validator.addMethod("username",function(value,element)
{
return this.optional(element) || /^[a-zA-Z0-9._-]{3,16}$/i.test(value);
},"Username are 3-15 characters");

$.validator.addMethod("password",function(value,element)
{
return this.optional(element) || /^[A-Za-z0-9!@#$%^&*()_]{6,16}$/i.test(value);
},"Passwords are 6-16 characters");

// Validate signup form
$("#signup").validate({
rules: {
email: "required email",
username: "required username",
password: "required password",
},
});
});
</script>
==========================================
HTML code
Contains simple HTML code.

<form method="post" action="thank.html" name="signup" id="signup">
Email<br />
<input type="text" name="email" id='email'/><br />
UserName<br />
<input type="text" name="username" id="username" /><br />
Password<br />
<input type="password" name="password" id="password" /><br />
<input type="submit" value=" Sign-UP " name='SUBMIT' id="SUBMIT"/>
</form>
==========================================
CSS code
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:13px;
}
input
{
width:220px;
height:25px;
font-size:13px;
margin-bottom:10px;
border:solid 1px #333333;
}
label.error
{
font-size:11px;
background-color:#cc0000;
color:#FFFFFF;
padding:3px;
margin-left:5px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}

 ==========================================

Tuesday, May 18, 2010

Advantages-of-JQuery?-or-Why- We-use-JQuery?

The first thing that most Javascript programmers end up doing is adding some code to their program, similar to this:

window.onload = function(){ alert("welcome"); }

Inside of which is the code that you want to run right when the page is loaded. Problematically, however, the Javascript code isn't run until all images are finished downloading (this includes banner ads). The reason for using “window.onload” in the first place is due to the fact that the HTML 'document' isn't finished loading yet, when you first try to run your code.

To circumvent both problems, here comes jQuery...Write Less, Do More, JavaScript Library.

Tuesday, October 27, 2009

Adding Images to TinyMCE Rich Text Editor using ASP.Net FileUpload Control

n my previous article I explained Using Tiny MCE Rich TextBox in ASP.Net

which simply describes how to add Tiny MCE Rich Text Editor in your ASP.Net Web form

Here I’ll be explaining how to add or upload images in your Tiny MCE RichTextBox along with other HTML Content.

I have already added a TinyMCE Editor to my page along with it I added the following controls

If you want to know more about how to add Tiny MCE RichTextBox to the ASP.Net Web Page you need to refer my previous article.


<form id="form1" runat="server">

<div>

<asp:Panel ID = "pnlEditor" runat = "server" >

<asp:TextBox ID="RichTextBox" runat="server" TextMode = "MultiLine" >asp:TextBox><br />

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />

asp:Panel>

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />

<asp:Button ID="btnCancel" runat="server" Text="Cancel" Visible = "false" OnClick="btnCancel_Click" />

<asp:Label ID="lblDisplay" runat="server" Text="" Visible = "false" >asp:Label>

div>

form>

I have added a Panel with three controls a TextBox which will be out RichTextEditor, a FileUpload control to upload pictures, images or graphics and an upload button to upload the pictures

Next I have Save and Cancel Buttons along with a Label which will display the Rich Text Content whenever the Save Button is clicked.

Once the controls are added your web page will look as below


Adding TinyMCE RichTextBox to ASP.Net Web Page


As you can see above there’s a FileUpload Control and an Upload Button so in order to insert picture or image in the Tiny MCE Rich text Box you’ll need to do the following on the Upload Button Click event. To store the images I have created a folder called images in the website root directory.

C#

protected void btnUpload_Click(object sender, EventArgs e)

{

if (FileUpload1.HasFile)

{

string FileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

string FilePath = "images/" + FileName;

FileUpload1.SaveAs(Server.MapPath(FilePath));

RichTextBox.Text += string.Format(" '{1}'", FilePath, FileName);

}

}

VB.Net

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)

If FileUpload1.HasFile Then

Dim FileName As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)

Dim FilePath As String = "images/" & FileName

FileUpload1.SaveAs(Server.MapPath(FilePath))

RichTextBox.Text += String.Format(" '{1}'", FilePath, FileName)

End If

End Sub

In the above code snippet I am simply saving the uploaded image file in the images folder and then creating an HTML image (IMG) tag and setting its SRC property to the path where the uploaded image or picture is saved. Finally I am appending the image tag to the RichTextBox Content.

Thus the TinyMCE Editor will now display the image or picture we uploaded along with the other HTML content. The figure below displays a Tiny MCE RichTextEditor with the uploaded Image embedded in it.


TinyMCE Editor displaying the uploaded image / picture in ASP.Net


Similarly you can display the HTML Content using a Label on a web page by placing the following code in the click event of the Save button

C#

protected void btnSave_Click(object sender, EventArgs e)

{

lblDisplay.Visible = true;

pnlEditor.Visible = false;

lblDisplay.Text = RichTextBox.Text;

btnSave.Visible = false;

btnCancel.Visible = true;

}

VB.Net

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)

lblDisplay.Visible = True

pnlEditor.Visible = False

lblDisplay.Text = RichTextBox.Text

btnSave.Visible = False

btnCancel.Visible = True

End Sub

As you will notice above I am simply assigning the contents of the Tiny MCE RichTextBox to an ASP.Net Label Control. The figure below displays the Label with the Rich Text Content


Label displaying Rich Text Content along with Image Picture in ASP.Net Webpage



The above code has been tested in the following browsers

Internet Explorer FireFox Chrome Safari Opera

* All browser logos displayed above are property of their respective owners.

 

Web Design Company karimnagar, Web Designing warangal, Logo Design Company nizamabad, Indian Website Design Company, maddysoft.co.in