Javascript Global scope problem :?

GT9

Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    මචන් මට පොඩි අවුලක් තියෙනව Javascript සම්බන්දව.... මෙන්න මේකයි ප්‍රශ්නේ..

    මම සරල form එකක් හදල ඒකේ text input field එකෙන් එන text values screen එකේ Print කරන්න තමයි උත්සාහ කරේ.. අපි Php වලදි සරලව $_GET[]; USE කරලා කරන්නේ අන්න ඒක JS වලදී කරන විදිය මම උත්සාහ කරා.. ඒත් පොඩි අවුලක් ආව.. මම හිතන්නේ අවුල තියෙන්නේ SCOPE එකේ ...යටින් තියෙනවා මම උත්සාහකරපු කෝඩ් එක..

    Code:
    <html>
    <head>
    </head>
    
    <body>
    
    <script>
    var text = document.form1.name.value;
    function printText(){	
    	document.write(text);
    }
    </script>
    
    
    
    
    <form name=form1>
    <input type="text" id="txt" name="name"   ><br><br>
    <input type="button" value="Submit!" onClick="printText();" >
    </form>
    </body>
    </html>

    මෙහෙම කරාම Button එක click කරාම undefined කියල තමයි screen එකේ print වෙන්නේ.. :nerd: ඒ කියන්නේ.. මම කෝල් කරපු var text කියන ‍variable එකට හඳුනගන්න බෑ form inputs...

    මම මෙහෙමත් ට්‍රයි කරා. එතකොට නම් වැඩ....

    Code:
    <html>
    <head>
    </head>
    
    <body>
    
    <script>
    
    
    function printText(){	
    var text = document.form1.name.value;
    	document.write(text);
    }
    </script>
    
    
    
    
    <form name=form1>
    <input type="text" id="txt" name="name"   ><br><br>
    <input type="button" value="Submit!" onClick="printText();" >
    </form>
    </body>
    </html>

    :P

    var text = document.form1.name.value; මචන් මෙන්න මේ ස්ටේට්මන්ට් එක function එකෙන් එලියට ගත්තම ඇයි වැඩ කරන්නේ නැත්තේ...? :oo: var text කියන වේරියබල් එකත් තියෙන්නේ ග්ලෝබල් අවස්තාවේ.. එතකොට ගැටලුව තියෙන්න ඕනේ...document.form1.name.value; මෙන්න මේ කෑල්ලේ.. මේ කෑල්ල කොහොමද මචන් global scope එකට ගේන්නේ...?

    දන්න කියන පින්වතෙක්..මේ පෝය දවසේ මට පිහිට වෙනු මැනවි... මහත්පල මහානිසන්ස ලැබේ... :P
     

    hunk

    Junior member
  • Oct 1, 2006
    282
    19
    18
    Use like this

    <script>
    var text = document.getelimentById("txt").value();
    function printText(){
    alert(text);
    }
    </script>
     

    rskoora

    Well-known member
  • Apr 1, 2008
    4,001
    608
    113
    You can't use global varible in this kind of situations ...

    reasons when u hit page refresh whole page page going to load once a again top to bottom ... at that time it will first execute your

    var text = document.form1.name.value; statement actually document.form1.name.value; does not exsists at the moment (htmls still to be generated) after html generated in DOMs ur printText() function will execute with undefined text wii print
     

    Twinkle24

    Member
    Jul 12, 2013
    1,093
    25
    0
    ~upon a cloud~
    function eken eliyata gaththama wada karanne naththe onclick eken call karanne function eka nisa.
    global scope ekata ganna nam script eka header ekata dala balanna
    <head>
    methanata danna script eka
    </head>
     

    Twinkle24

    Member
    Jul 12, 2013
    1,093
    25
    0
    ~upon a cloud~
    You can't use global varible in this kind of situations ...

    reasons when u hit page refresh whole page page going to load once a again top to bottom ... at that time it will first execute your

    var text = document.form1.name.value; statement actually document.form1.name.value; does not exsists at the moment (htmls still to be generated) after html generated in DOMs ur printText() function will execute with undefined text wii print

    thanks. It's useful for me too :yes:
     

    GT9

    Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    function eken eliyata gaththama wada karanne naththe onclick eken call karanne function eka nisa.
    global scope ekata ganna nam script eka header ekata dala balanna
    <head>
    methanata danna script eka
    </head>

    naa machan eka nemei awla , BTW thanks :)

    You can't use global varible in this kind of situations ...

    reasons when u hit page refresh whole page page going to load once a again top to bottom ... at that time it will first execute your

    var text = document.form1.name.value; statement actually document.form1.name.value; does not exsists at the moment (htmls still to be generated) after html generated in DOMs ur printText() function will execute with undefined text wii print

    oh! machan.. thanks a lot mama therum gaththa awla.. ammo loku udawwak kare... :)
     

    mldarshana

    Well-known member
  • Apr 2, 2007
    34,059
    1,404
    113
    ආශ්චර්ය අභියස :nerd:
    try this type of a method

    PHP:
    <html>
    <head>
    </head>
    
    <body>
    
    <script>
    	var text = '';
    
    	function runValidate(){	
    		text = document.getElementById('txt').value;
    		if(text == ''){
    			alert('Please enter a value');
    			return false;
    		} else {
    			document.getElementById('error').innerHTML = text;
    			return false;
    		}
    		
    	}
    </script>
    
    <p id="error" style="color:red;"></p>
    <form name="form1" action"" method="post" onsubmit="return runValidate();">
    	<input type="text" id="txt" name="name"><br><br>
    	<input type="submit" value="Submit!" >
    </form>
    </body>
    </html>
     

    GT9

    Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]

    වෙනද වගේම අදත් මාව ගොඩ දැම්මේ ඔයා... :)

    oyata thanks kiyanna one. mamath meken deyak igena gaththa :yes:

    එළ එළ...:)

    try this type of a method

    PHP:
    <html>
    <head>
    </head>
    
    <body>
    
    <script>
    	var text = '';
    
    	function runValidate(){	
    		text = document.getElementById('txt').value;
    		if(text == ''){
    			alert('Please enter a value');
    			return false;
    		} else {
    			document.getElementById('error').innerHTML = text;
    			return false;
    		}
    		
    	}
    </script>
    
    <p id="error" style="color:red;"></p>
    <form name="form1" action"" method="post" onsubmit="return runValidate();">
    	<input type="text" id="txt" name="name"><br><br>
    	<input type="submit" value="Submit!" >
    </form>
    </body>
    </html>

    රිප්ලයි එකක් දැම්මට ගොඩක් තෑන්ක්ස් මචන්.. මම Js වලට ගොඩක්ම අලුත්.. මේ page refreshing, reload ව‍ගේ දේවල් මම දන්නෑ.. මම Php වලින් ට්‍රයි කරපු දේවල් මේවගෙනුත් ට්‍රයි කරා.. අද ලොකු point එකක් ඉගෙන ගත්ත.. මම හිතන් හිටියේ මේක scope එක සම්බන්ද අවුලක් කියල.. :D
     

    conzy

    Well-known member
  • Mar 11, 2010
    1,890
    1,781
    113
    ඔතන අවුල තියෙන්නේ variable scope එකේ නෙවෙ. onClick method එකෙන් call වෙන්නේ printText() function එක. ඒ function එක ඇතුලේ text variable එකට value එකක් assign වෙලා නැ එකයි. ඔයා text variable එකට input value එක දීල තියෙන්නේ document එක load වෙන වෙලාවේ. ඒ වෙලාවේදී document.form1.name එකට value එකක් නැ. එකයි undefined කියල එන්නෙ.