Skip to main content

Posts

Showing posts with the label javascript

Why Why there is no Trim function on JS

It was pretty bad when you find that the JS dos not have a inbuilt function on strings to do Trim. But not to worry. It can be implemented easily. function LTrim(str){ var whitespace = new String(" \t\n\r"); var s = new String(str); if (whitespace.indexOf(s.charAt(0)) != -1) { var j=0, i = s.length; while (j < i && whitespace.indexOf(s.charAt(j)) != -1) j++; s = s.substring(j, i); } return s; } function RTrim(str){ var whitespace = new String(" \t\n\r"); var s = new String(str); if (whitespace.indexOf(s.charAt(s.length-1)) != -1) { var i = s.length - 1; while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) i--; s = s.substring(0, i+1); } return s; } function Trim(str){ return RTrim(LTrim(str)); }

JQuery API control functions

Image from hoth.entp.com I was doing JavaScript for few years and it was little bit hard to work with "write less, do more" at first few times. Always it comes to my mind as JavaScript (yes I know it is) so I am trying to apply some functions as document.getElementById. And when I wanted to hide and show elements it was style:none and block.Those look bad(when a better way is there) on jQuery and some of them may not work on jQuery elements. OK I am not here to teach you neither JavaScript nor jQuery. For the people like me it is strongly recommended (by me of course) to take a look at the available and right functions on jQuery API official site .t I just found that and thought to share that. If you got a better place or some useful links please share it.

How to call a web method by JavaScript

I didn’t get a time to write something here for a long time. Yes I can say I was busy. (But I was not :)) Anyway I thought to write about something finally and I promise (this is a serious promise) that I will try to do something (do a posting actually) on my blog more frequently. The ability to call the web methods by JavaScript on .net is very very useful and we can implement the web pages more efficiently by using this. For example we do not need to send whole page and update. Yes this is AJAX (that what I think). Here is a web method example. It should be on the aspx page code behind and should be a static (shared) method. I have tried to insert the web methods on user controls and was unable to do so (if you know a way please share with us). This is on VB.net. _ Public Shared Function GetMyData(ByVal studentID As Integer, ByVal schoolID As Integer) As Student Dim studentService As New StudentService() Dim result As Student = studentService.GetStudent(studentI...

JavaScript confirmation disables validation

I had to do a JavaScript confirmation before I perform the post back on a web form. What I have done is just call button.attributes.add("onClick", -- ) and added the confirmation. It worked fine and showed the popup and did the post back if I clicked on the OK button. But if I have implemented some validations on the fields they doesn't fire them if the validation fails. For example if there is a required field validator the post back happens even without the values for that required field. what I want is to do the validation and then do the post back. Luckily I've found a solution at http://forums.asp.net/t/963412.aspx?PageIndex=1 by sukdeb . function Validate() {     var yes=confirm("Update this application?");     if(yes)     {         Page_ClientValidate();         return Page_IsValid;     } return false; } <asp:button runat="server" text="Button" onc...

Show an animated gif onClientClick

To show an animated gif, something like loading onClientClick is a simple task. You just have to add a JavaScript function on the click event so the JS function will take care until the next page loads from the server. Inside the JS function we just have to hide the excising div and show the div which contains animated loading gif. I have done it and it works fine on every other browser except IE. On IE the gif is shown but it is not animated. I have to do some R&D and what I found was IE doesn't do anything like that when it is loading another page (communicating with server) and was commented as a feature limitation of IE. But there should be a way to do that, that's what I was thinking and I found a pretty simple way to do that. Here it is and if you have a better way please share it here. <div id="divProgress" runat="server">   </div> <div id="Div1" runat="server"> The web form comes here … </div>   The J...

Disable Back & RegEx Validation: JS

Disable Back on Browser: JavaScript I know disabling back button of a browser may cause usability issue. I have identified on some occasions we have to do it (what’s if client needs it). I have found some ways to do that but I preferred using a simple JavaScript. Here is the code. < script type ="text/javascript" > < !-- window.history.forward(1); //--> < / script > This should be put on the previous page to the page you wish to disable the back. This code will redirect user to the same page if he click back button. Regular expression validation on JavaScript I know it is very much easy to validate something on JavaScript by using Regular expressions but it is hard for me to remember the syntax to do it. I’m posting this for me to get when it is needed. J Here is the Code. < script type ="text/javascript" > < !-- function validateDate(source, clientside_arguments) { ...

Working with forms inside iframes

I was requested to do some work with forms using the iframes on a web page and it was needed to do a lot of Googling and thought to share those things. 1. Display a form on the main page and when the form is submitted the results should be shown on an iframe on the same page. a. This task was pretty simple and what I had to do is give the same page as the action page of the form. b. If the form data is defined on the page I had to show the iframe.When showing the iframe I had to give all the form data as query parameters on the URL of the iframe. c. Then the resulting page was displayed correctly inside the iframe. 2. Then the resulting page which was inside the iframe was also a web form and it has to be submitted on the event of a button click which was on the main page. a. For the button click event we have to fire a JavaScript function. b. On that JavaScript function we can do the submission as follows. c. This will be doing the submission and we have to redire...

Replace new line with <br> tag

  Most of the multi line text areas we are using the user can input text by using line breaks. If that text used for internal purpose saving it as a single string may not be a problem.   But what's if we have to show that on a web page (forums, user comments, etc). We have to replace the new line character with the <br> tag. The famous way is to replace chr(13) with the <br> tag by using the Java Script. But the problem is this method is not working fine with every platform. But I have found a way to come up with and thought to share it with you.   function escapeVal(textarea,replaceWith) { textarea . value = escape (textarea . value)          for (i = 0 ; i < textarea . value . length; i ++ ) { if (textarea . value . indexOf( "%0D%0A" ) > - 1 ) {     textarea . value = textarea . value . replace( "%0D%0A" ,replaceWith) } else if (textarea . value . indexOf( "%0A" ) > - 1 ) {    ...

Special characters on web pages

It was a real pain for me to work with special characters on a web page. Yes we know some of them like "&nbsp; , &amp;" but what happens if we are asked to put ¼ or õ we don't know what is the html name of that character. This happens frequently when working with languages other than English. So I found a site which has whole a lot of information on that. Here it is http://ascii.cl/htmlcodes.htm . Here are some of them. à á â ã ä å æ ç è é ê ë ì í î ï &#224; &#225; &#226; &#227; &#228; &#229; &#230; &#231; &#232; &#233; &#234; &#235; &#236; &#237; &#238; &#239; &agrave; &aacute; &acirc; &atilde; &auml; &aring; &aelig; &ccedil; &egrave; &eacute; &ecirc; &euml; &igrave; &iacute; &icirc; &iuml; latin small letter a with grave latin small letter a with acute latin small letter a with circumflex latin small letter a with tilde latin small lett...

window.onbeforeunload on IE 7

I was tried to track when a user is just going to away from a web page by using JavaScript. That was pretty possible with the window.onbeforeunload it worked fine. But fine until my project QA team found that it does not work with Internet Explorer 7. window.onbeforeunload is working with the Firefox and IE 6 but it does not work with IE 7 (in the same manner with other browsers). So I was able to find a solution for that. var goodExit = false; window.onbeforeunload = confirmExit; function = confirmExit() { if(your any condition) { goodExit = true; //then that confirmation will not be asked } if(!goodExit) { return 'Are you sure to navigate away ?'; //this will display on confirmation } } //there should not any returns on the function confirmExit. You can make the goodExit true when the page is doing submit or any other that we do not need a confirmation.And also do not assign true or false to window.onbeforeunload. Because it may lead to a ...