SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Avoiding namespace variable collision - help please

    I understand the need to attempt to avoid namespace collision with variable names. I also 'get' the way that YUI attempts to implement it i.e. prefixing all their objects with YAHOO.

    Problem is, I don't quite understand how to implement it myself e.g.

    Here's some primitive validation code (i don't like lots of nested if's !) that sits it it's own little file ... can someone tell me what I need to do to implement it in the same way as YUI?

    Thanks in advance.

    Code:
    function check() {
    	
    	var descr =document.getElementById('descr').value;
    	var price =  document.getElementById('price').value;
    	
    	var valflag;
    	valflag="Y";
    	
    
    	if (descr.length < 1)
    		{
    			alert("Enter a Description.");
    			valflag="N";
    			document.forms[0].descr.focus();
    		}
    		
    		
    	if (price.length < 1 && valflag=="Y")
    		{
    			alert("Enter a Price.");
    			valflag="N";
    			document.forms[0].price.focus();
    		}	
    		
    	if (!IsNumeric(price) && valflag=="Y")
    		{
    			alert("Enter a Valid Price.");
    			valflag="N";
    			document.forms[0].price.focus();
    		}		
    		
    }
    
    // Generic function to check for numeric input			
    function IsNumeric(ParText)
    
    {
       var ValidChars = "0123456789.";
       var IsNumber=true;
       var Char;
     
       for (i = 0; i < ParText.length && IsNumber == true; i++) 
          { 
          Char = ParText.charAt(i); 
          if (ValidChars.indexOf(Char) == -1) 
             {
             IsNumber = false;
             }
          }
       return IsNumber;
    }
    and, would I have to change the php code to? currently it's set up as:

    Code:
    <form name=form01 id=form01 method=POST action=".$_SERVER['PHP_SELF']." onSubmit=".chr(39)."return check()".chr(39).">";
    Last edited by wellyfish; May 17, 2008 at 01:18. Reason: ..

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use the following technique..

    Code JavaScript:
    var mynamespace = new object();
        mynamespace.check = function() {
            //.. your code
        }
     
    <form name=form01 id=form01 method=POST action=".$_SERVER['PHP_SELF']." onSubmit=".chr(39)."return mynamespace.check()".chr(39).">";


  3. #3
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    You could use the following technique..

    Code JavaScript:
    var mynamespace = new object();
        mynamespace.check = function() {
            //.. your code
        }
     
    <form name=form01 id=form01 method=POST action=".$_SERVER['PHP_SELF']." onSubmit=".chr(39)."return mynamespace.check()".chr(39).">";
    So that's the best technique to use then?

    Great!

    and then I can reference my 'own' variables like:
    Code:
    	mynamespace.descr =document.getElementById('descr').value;
    	mynamespace.price =  document.getElementById('price').value;
    and

    Code:
    if (mynamespace.descr.length < 1)
    Is that how it would go??

    Thanks...

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You got it! There may better ways, but this way seems to work for me.


  5. #5
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just wrap everything around in a self executing anonymous function..

    Code:
    (function() {
    // your code
    })();

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2007
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so sticking all of it in a function would keep the scope of the variables within that anonymous function, right?

    Is one way better than another?

    Cheers.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    If it will be entirely self-contained then the self-executing anonymous function is a good way to go. It's also a nice reminder that you don't intend to get information from inside of there.

    Using the namespace idea can be helpful with larger projects, beacuse you can store information and functions in there, and easily access them later on.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •