Get URL GET values using javascript

I had a situation of getting URL GET values to my web page without using server side scripting language like PHP, JSP or ASP.

So this is a simple way to get URL GET value to your web page using javaScript. When you don’t have any facilities to code server side scripts, you can use this technique to make your website more dynamic.

Simple example :

<html>
  <head>
     <script>// Import GET Vars
        (function(){
           document.$_GET = [];
           var getVarsString = String(document.location).split('?');
           if(getVarsString[1]){
              var getVars = getVarsString[1].split('&');
              for(var i=0; i<=(getVars.length); i++){
                 if(getVars[i]){
                    var getVarPair = getVars[i].split('=');
                    document.$_GET[getVarPair[0]] = getVarPair[1];
                 }
              }
           }
         })();
         function updateView(){
            document.getElementById("view").innerHTML=document.$_GET['i'];
         }
     </script>
  </head>
  <body>
      <input id="txt" type="button" value="click" onclick="updateView();" />
      <div id="view"></div>
  </body>
</html>
Advertisements

solution for prototype.js and jquery.js conflict problems

Jquery have provide a solution for prototype.js and jquery.js conflicts. There is one caveat: By default, jQuery uses “$” as a shortcut for “jQuery”. However, you can override that default by calling jQuery.noConflict() at any point after jQuery and the other library have both loaded. For example:

<script src=”prototype.js”><⁄script>
<script src=”jquery.js”><⁄script>
<script>
var $j =jQuery.noConflict();
$j(document).ready(function(){

⁄ ⁄ Use jQuery via $j(…)
$j(‘#button1’).click(function(){
var textbox1 = $j(‘#textbox1’).val();
alert(textbox1);

});

⁄ ⁄ Use Prototype with $(…)
$(‘someid’).hide();

}
<⁄script>

How to detact javascript is enable or not in the client browser

There are various kind of solution to check the javascript support of client browser. In here I’ll provide a solution to block client access who have not enabled javascript in his/her browser.

In this example I user index.html which clients want to visit. And user enable_js.html which will redirect clients from index.html page when their browser not support to javascript.

<noscript>
<meta equiv=”refresh” content=”0;URL=enable_js.html”>
</noscript>

Add above code into the body (between body tags) of index.html. This part will redirect clients who user browsers which not support for javascript to enable_js.html. If javascript is enable, then nothing happent from this code.


<h2 id=”nojs” style=”font-family: Verdana,Arial,Helvetica,sans-serif; color: rgb(204, 0, 0);”>
JavaScript is turned off in your web browser. Turn it ON (Enable) to access the website, then refresh the page.
</h2>
<script>
document.getElementById(“nojs”).style.display=”none”;
alert(“Javascript enabled successfully! Press ok to continue”);
window.location = “index.html”;
</script>

Add above code into the body of enable_js.html. This will display error massage when javascript is disable in client’s browser. After enable javascript and refresh this page will provide alert with informing the javascript is successfully working. And after press ok it will redirect to the index.html.

javascript mail validation (check)

var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(txtEmail.value)) {
alert(‘Please provide a valid email address’);
txtEmail.focus();
return false;
}
use this javascript function to validate email addresses.
%d bloggers like this: