Skip to main content

Posts

Showing posts with the label JavaScript

Check if the browser is safari using javascript

For checking if the browser is safari or not you can use below javascript function. function isSafari() {      var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;      if(is_safari)     {          return true;     }     else     {         return false;     } } The above method will return true or false depending on the browser. i.e.  if the browser is safari then the above method will return true and if it's not then the method will return false. Hope this will help :)

Get Selected value from Listbox Javascript

Since there is no direct way to get the selected value in the listbox you have to get it by iterating over the list.For same the code is given below. Below code will print all values in the listbox which were selected but you can modifiy the code as per your requirement. HTML code : <select id="listboxName"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> Javascript code : var mySelect = document.getElementById("listboxName"); for ( var i = 0 ; i < mySelect . options . length ; i ++) { if ( mySelect . options [ i ]. selected ) document . write ( " mySelect.options[i].text\n" ) } Hope this helps :)

Enabling/Disabling Textbox using JavaScript

Below is the example for enabling or disabling of the text box whose id is "testTextbox". HTML code of the text box <p>   <input type="text" id="testTextbox" ></input> </p> Javascript code for enabling the text box document.getElementById("testTextbox").disabled = false; Javascript code for disabling the text box document.getElementById("testTextbox").disabled = true; you can add the above javascript code in any of the javascript function you want and call it on some event or you can keep them disabled by keeping the input tags attribute to disabled like this they will always remain disabled and then you can enable them on some event

Ternary Operator in Java and Javascript

Ternary Operators are basically replacement for if-else condition. Below is the example for java: if(condition == true) {     System.out.println("Condition is true"); } else {    System.out.println("Condition is false"); } The above example can be replaced by a ternary operator as : (condition == true)?System.out.println("Condition is true"):System.out.println("Condition is false"); The syntax remains almost same for javascript : if(condition == true) {     alert("Condition is true"); } else {    alert("Condition is false"); } (condition == true)?alert("Condition is true"):alert("Condition is false"); Ternary Operators reduces the readability of the code little bit but they give a better performance. If you are using if-else condition and there are millions of records to be processed then go for ternary operator.

Set div background image from javascript

While working with JavaScript and html many time this happens that you need to set some CSS property via  JavaScript  .One of the scenario that I faced was setting div's background image via a  JavaScript  function. I had following code : <div id="tree" style="width:200px; height:150px;">    Test data  </div> Now while loading the page I had few condition depending on that it should set the background image of the div.So, I created a function in JavaScript and called it onLoad of the body tag in the html page. That JavaScript method is as following : function changeDivBackground() {      document.getElementById("tree").style.background = "url('images/backgroundImage.jpg')"; } This will set the background image for the div.