Saturday, March 27, 2010

Get URL Parameters Using Javascript Sample Code

function getUrlParameter( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}


if the URL is http://www.foo.com/index.html?bob=123&frank=321&tom=213#top


then you can read parameter value of frank by


var valueOfFrank = getUrlParameter('frank');




Original article from : http://www.netlobo.com/url_query_string_javascript.html

Tuesday, March 23, 2010

Changing td (or table) background image to another from JavaScript

 <html>
  <head>
    <style>
      #Good {
        background-repeat: no-repeat;
        background-position: center center;
        background-image: url(images/image40.gif);
      }
    </style>
    <script>
      <!--
      function changeImage() {
        newImage = "url(images/image41.gif)";
        document.getElementById('Good').style.backgroundIm  age = newImage;
      }
      //-->
    </script>
  </head>
  <body>
    <table>
      <tr>
        <td id="Good"><br>Hopefully there is a nice picture behind me :P</td>
      </tr>
      <tr>
        <td><button onClick="changeImage()">Click Me</button><td>
    </table>
  </body>
</html>

Substring ( part of a string ) JavaScript

var str = 'im feeling ?? ';
from = 3;
t0 = 10;
str.substring(from, to);

Finding length of a string in JavaScript

var str="Im feeling stupid";
alert(str.length);

Sample Code Mouse Coordinates - JavaScript

 function checkwhere(e) {
        if (document.layers){
         xCoord = e.x;
         yCoord = e.y;
  }
        else if (document.all){
         xCoord = event.clientX;
         yCoord = event.clientY;
  }
        else if (document.getElementById){
         xCoord = e.clientX;
         yCoord = e.clientY;
  }
  if (shipSize>0){
   document.getElementById('movingShips').style.zIndex = -1;
   document.getElementById('movingShips').style.top  = yCoord-10+'px';
   document.getElementById('movingShips').style.left = xCoord-10+'px';
   document.getElementById('movingShips').style.position = 'absolute';
  }
      document.getElementById('divCoord').value = "X= "+ xCoord + "  Y= " + yCoord;  
 }
 document.onmousemove = checkwhere; // Calling the Mouse Coordinate Fantion

How to Disable the Submit Button

How to Disable the Submit Button using JavaScript
onclick="this.disabled=true"
eg:
<button onclick="setShips(5); this.disabled="true"">The Great Ship</button>