SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast erickkz's Avatar
    Join Date
    Dec 2005
    Location
    Earth
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Little problem. help!

    Hi everyone!

    When I load my page I have one container(called canvas),a button and a textarea (called svgcontent), when I press the button, my info is shown in my canvas container:

    PHP Code:
    <script language="JavaScript">
    var 
    mycounter=0;

    function 
    executetest(){
    if(
    document.getElementById('svgcontent').value!=""){
      
    withoutbackslashes=document.getElementById('svgcontent').value.split("\\");
      while(
    withoutbackslashes[mycounter]!=null){
        
    finalstring+=withoutbackslashes[mycounter];
        
    mycounter++;
      }
        
    //alert(finalstring);
        
    canvas.innerHTML=finalstring;
    }
    }
    </script> 
    But when I try to do it directly IE gives an "unknown error in execution time on the line withoutbackslashes=document.getElementById('svgcontent').value.split("\\");"
    PHP Code:
    if(document.getElementById('svgcontent').value!=""){
      
    withoutbackslashes=document.getElementById('svgcontent').value.split("\\");
      while(
    withoutbackslashes[myconter]!=null){
        
    finalstring+=withoutbackslashes[mycounter];
        
    mycounter++;
      }
        
    //alert(finalstring);
        
    canvas.innerHTML=finalstring;

    Strange, but in both cases I can get "finalstring" correct value alert(finalstring).
    Someone can help me?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your while has a typo, its not because of that is it?

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But when I try to do it directly
    html elements do not exist until the browser reads the lines that define them. So if you have this setup:
    Code:
    <head>
    <script>
    
    document.getElementById('svgcontent')...
    
    <script>
    </head>
    <body>
    
    <div id="svgcontent">blah blah blah</div>
    
    </body>
    When document.getElementById() executes, the browser has no idea what html is specified below, so as far as the browser is concerned there is no element with an id 'svgcontent'. You have to wait until after the page loads before the browser knows the div exists. Typically, you would do this:

    Code:
    <script>
    
    window.onload=function()
    {
    	document.getElementById("svgcontent")
    };
    
    </script>
    </head>
    <body>
    ...
    The code inside that function won't execute until after the page loads and then the browser knows about all the html elements specified in the <body> tag.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by erickkz View Post
    while(withoutbackslashes[mycounter]!=null){
    That is not a good way to find the end of an array. An out-of-range subscript will return 'undefined', not null;
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Enthusiast erickkz's Avatar
    Join Date
    Dec 2005
    Location
    Earth
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thx everybody! that gave me an idea..

    solved with onload:
    setTimeout ("executetest();", 1000);


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
  •