Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: var code line stopping the rest code from printing hello world

  1. #11
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi styxlawyer

    you tested it in which browser ??

    In firefox it didnt stop processing

    in Chrome on pressing submit button it show a blink of "hello world" and then "hello world" get disappear

    i even tried
    for(i=0;i<pp;i++)

    vineet

  2. #12
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    One of the quirks of only having one text input in a form is that it is continually being tested, so the page appears to be constantly refreshing. Also, pressing the enter key will submit the form but not run the JavaScript. Both of these "features" can be fixed by adding a second text input which is not displayed. Here's a working example of what I think you are trying to achieve:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function myfunc()
    {
    	var newContents ="";
      var pp = document.getElementById("formInput").value;
    	if(isNaN(pp)) {
    		newContents = "Please enter a numeric value."
    	}
    	else {
    		var n = parseInt(pp);
    		newContents = "You entered the number '"+n+"'.";
    	}
      document.getElementById("placeHolder").innerHTML = newContents;
    }	
    </script>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform">
    <input type="text" name="name" id="formInput" value="" />
    <input type="text" style="display:none;" />
    <input type="button" value="submit" onclick="myfunc();" />
    </form>
    <p>&nbsp;</p>
    <p id="placeHolder">&nbsp;</p>
    </body>
    </html>

  3. #13
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That might work. But since what we both see as the problem is that the form submits, thereby reloading the page. I would simply add a return false; to the submit function so that it doesn't submit and reload the page:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc(); return false;">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = +document.pform.name.value;
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){ break; }
    		}
    	}
    }	
    </script>
    </body>
    </html>
    It depends upon what you want to happen, if anything, aside from running the code, and you haven't really told us that yet. There are other ways of dealing with this, again, depending upon what all you want to have happen.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #14
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi styxlawyer

    i added forloop in your provided code

    it works great only in chrome

    But in firefox it keeps on processing after printing "hello world"

    my firefox version is 48.0.2

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function myfunc()
    {
    	var newContents ="";
      var pp = document.getElementById("formInput").value;
    	if(isNaN(pp)) {
    		newContents = "Please enter a numeric value."
    	}
    	else {
    		var n = parseInt(pp);
    		newContents = "You entered the number '"+n+"'.";
    		for(i=0;i<pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){ break; }
    		}
    	}
      document.getElementById("placeHolder").innerHTML = newContents;
    }	
    </script>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform">
    <input type="text" name="name" id="formInput" value="" />
    <input type="text" style="display:none;" />
    <input type="button" value="submit" onclick="myfunc();" />
    </form>
    <p>&nbsp;</p>
    <p id="placeHolder">&nbsp;</p>
    </body>
    </html>

  5. #15
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi john

    i tested your solution of return false

    it works great perfect on chrome

    but again in firefox it keeps on processing after printing "hello world"

    my firefox version is 48.0.2

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc(); return false;">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = +document.pform.name.value;
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){ break; }
    		}
    	}
    }	
    </script>
    </body>
    </html>

  6. #16
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    The code I posted in message #12 works fine in Opera, Firefox and Chrome. It fails to print anything in IE, but that doesn't surprise me at all. Does the code I posted work in your copy of Firefox before you make any changes to it?

  7. #17
    Join Date
    Nov 2011
    Posts
    65
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by styxlawyer View Post
    The code I posted in message #12 works fine in Opera, Firefox and Chrome. It fails to print anything in IE, but that doesn't surprise me at all. Does the code I posted work in your copy of Firefox before you make any changes to it?
    hi styxlawyer

    yes your original code of post #12 which doesnt have forloop works fine in my firefox

    but when i add forloop in it , then it doesnt stop processing in firefox


    vineet

  8. #18
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I did a demo, and I see what I missed. Once the page is loaded, using document write obliterates the existing page and creates a new one. In Firefox I guess you have to close the new document in order to stop the loading. However, I would use something other than document.write. Create nodes or use the innerHTML property of an existing element. That said, closing does work to stop the loading:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc(); return false;">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = +document.pform.name.value;
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){document.close(); break; }
    		}
    		
    	}
    }	
    </script>
    </body>
    </html>
    One can get a similar effect without document.write:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc(); return false;">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = +document.pform.name.value, op = '';
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		op += "Hello World <br>\n";
    			
    			if(i === pp){document.forms[0].innerHTML = op; break; }
    		}
    		
    	}
    }	
    </script>
    </body>
    </html>
    Or, without obliterating the form:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc(); return false;">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <div id="output"></div>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = +document.pform.name.value, op = '';
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		op += "Hello World <br>\n";
    			
    			if(i === pp){document.getElementById('output').innerHTML = op; break; }
    		}
    		
    	}
    }	
    </script>
    </body>
    </html>
    Or, by creating nodes:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form action="" method="post" name="pform" id="pform" onsubmit="myfunc(); return false;">
    <input type="text" name="name" id="uniqueID" value=""/>
    <input type="submit" value="submit" />
    </form>
    <div id="output"></div>
    <script type="text/javascript">
    function myfunc()
    {
    //var pp = document.getElementById("pform").getAttribute("name").value;
    var pp = +document.pform.name.value, op = document.getElementById('output');
    if(pp > 0)
    	{
    		//alert(pp);
    		for(i=0;i<=pp;i++)
    		{
    		op.appendChild(document.createTextNode("Hello World"));
    		op.appendChild(document.createElement('br'));
    		op.appendChild(document.createTextNode("\n"));
    			
    			if(i === pp){ break; }
    		}
    		
    	}
    }	
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 09-12-2016 at 03:18 PM. Reason: add alternatives
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #19
    Join Date
    Nov 2014
    Location
    On A Scottish Island
    Posts
    488
    Thanks
    0
    Thanked 62 Times in 58 Posts

    Default

    OK, so there's something wrong with your 'for' loop. Here's the code:

    Code:
    		for(i=0;i<=pp;i++)
    		{
    		document.write("Hello World <br>");
    			
    			if(i === pp){ break; }
    		}
    There are two errors which are immediately obvious. Firstly the variable 'i' used as the loop counter is not declared anywhere in your code and secondly the line 'if(i === pp){ break; }' is redundant as 'i' will never reach the value in 'pp'. The following is sufficient and should work:

    Code:
    		for(var i=0; i<pp; i++)
    		{
    		  document.write("Hello World <br>");
    		}

  10. #20
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You're right. the var i should be declared, but in this code, that doesn't matter. Your not 100% right about the break statement, it can be useful, though, if all you're doing with it is breaking, then it is redundant. I've solved the actual problem, which is using document.write and not following it with document.close. See:

    http://www.dynamicdrive.com/forums/s...164#post319164
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Help with one line of code in Expando
    By porkmeister in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 04-12-2009, 05:41 PM
  2. phpbb3 and the rest of the world
    By Dirt_Diver in forum PHP
    Replies: 3
    Last Post: 02-06-2009, 12:20 PM
  3. Printing only an image, not the rest of the page
    By venu_1200 in forum JavaScript
    Replies: 1
    Last Post: 08-20-2008, 09:52 AM
  4. Printing iframe from DD Tabbed doc code
    By its_jon in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 04-05-2008, 08:50 AM
  5. Easiest Language [real world, not code]
    By djr33 in forum The lounge
    Replies: 79
    Last Post: 11-26-2007, 04:12 PM

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
  •