Friday, 25 April 2014

Remove HTML tags from string but keep content.

Introduction:

Here I will explain how to remove specific HTML tag from string but keep content which is inside tag.

In JQuery we can remove element by remove()  function but how would we remove specific HTML tag from string.

Just need to use unwrap() function of JQuery.

Code:
$('p').contents().unwrap();



Example:






Suppose we want to remove all p page from string.

var temp='<div>Some text</div><p style="color:red">More text<span>here</span></p><p>Even more</p>';

var StrTemp=$(temp).find('p').contents().unwrap();

alert(StrTemp);



This will alert "<div>Some text</div>More text<span>here</span>Even more"
unwrap functino remove all p tag from string.



Monday, 31 March 2014

JQuery Check element is block or hidden.

Introduction:

Here I will explain how to check any element like div,p,input is block or hidden.

In JQuery we can show/hide element by show(), hide() functions but how would we test element is visible or not so here is solution for check visibility of element.

Just need to check visible property.

Code:
$(element).is(":visible");


Example:





Suppose you want to check visibility of div element then just select div.

var b = $('div.test').is(":visible");;
alert(b);


This will alert true if div with class test is visible and return false if div with class test is hidden.



JavaScript get query string values

Introduction:

Here I will explain how to get Query String value from URL in JavaScript.

Code:
function getParameterByName(name) {
 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
 results = regex.exec(location.search);
 return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


Example:





Suppose you have three Query String in current URL a b and c and you need value of b so just need to pass b in this function.

var b = getParameterByName('b');
alert(b);

This will alert value of b.



Monday, 24 March 2014

Change Default Port Number in Visual Studio 2012

Step 1
Right Click on the web site or project node in Solution Explorer.

Step 2
Select properties from options.



Step 3
Choose Web option.

Step 4
Choose Use Visual Studio Development Server Radio button.

Step 5
And Then Choose Specific Port, Now a Text Box is enable and you can set port number to whatever you need.





Thursday, 2 January 2014

MVC 4 New Feature add Controller to other project folder.

In Mvc 3, controller class can be added only in the "Controllers" folder ,adding controller class to other folder is not allowed but in Mvc 4, we can add controller class to other folder.

Step 1: Create a "Data" folder inside "Controllers" folder.

Step 2: Add Controller inside Data folder.




Wednesday, 1 January 2014

Implement Simple Membership , Creating Role & Register admin account With Simple Membership MVC 4

For Creating Role,Login,Registration first we have to Initialize simple membership.
For implement simple membership follow 2 easy steps.

Step 1: Initialize membership in Global.asax file. Copy this function in Global.asax
void initializeMembership()
        {
            if (!WebSecurity.Initialized)
                WebSecurity.InitializeDatabaseConnection("ConnectionString", "UserProfile", "UserId", "UserName", autoCreateTables: true);

        }
And Call this function in Application_Start().
This function create Simple Membership tables in given "ConnectionString" and initialize membership.

Step 2: Add settings in web config.
<profile defaultProvider="SimpleProfileProvider">
      <providers>
        <add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" 
             connectionStringName="ConnectionString" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="SimpleMembershipProvider">
      <providers>
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
      </providers>
    </membership>
    <roleManager defaultProvider="SimpleRoleProvider">
      <providers>
        <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
      </providers>
    </roleManager>
Now your simple membership tables are created and simple membership is Initialized.

Creating Role


For Creating Role we use System.Web.Security.Roles Namespace
var role = System.Web.Security.Roles.Provider;
            if (!role.RoleExists("Admin"))
            {
                role.CreateRole("Admin");
            }

Register Admin Account


For Register Admin we use WebMatrix.WebData.WebSecurity namespace
  WebSecurity.CreateUserAndAccount("Admin", "admin123");
add this user to admin role.
   role.AddUsersToRoles(new[] { "Admin" }, new[] { "Admin" });
First Parameter usernames and second roles.


Thursday, 12 September 2013

Create a endsWith Function in JavaScript.

Javascript does not have a endsWith function so we have to create it manually we use String prototype for create endsWith function.
if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
    };
}
Then you can use it directly on string values.
var data = "Lorem ipsum dolor sit amet";
data.endsWith('amet'); //true