Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Nested document.writeln() question...

    Hi:

    I'm still wrestling with the same problem:

    Is there any way to pass a variable in a nested document.writeln() statement???

    Example Script:


    <script>
    <!--

    myImg=new Image()
    myImg.src="http://www.ekigroup.com/javascript/Clipboard.jpg";
    myImg="clipboard";
    function passVar(){
    document.open();
    document.writeln("Your image name is " +myImg);
    document.writeln("<script>");
    document.writeln("<!--");
    document.writeln("function showDoc(){");
    document.writeln("myImg=\"clipboard\";");
    document.writeln("document.open();");
    document.writeln("document.writeln(\"Your image name is \" +myImg);");
    document.writeln("document.close();");
    document.writeln("}");
    document.writeln("setTimeout(\"showDoc()\",5000);");
    document.writeln("//-->");
    document.writeln("<\/script>");
    document.close();
    }

    //-->
    </script>

    <body onload="passVar();">


    The second document output:


    Your image name is clipboard
    <script>
    <!--
    function showDoc(){
    myImg="clipboard";
    document.open();
    document.writeln("Your image name is " +myImg);
    document.close();
    }
    setTimeout("showDoc()",5000);
    //-->
    </script>


    The third document output:

    Your image name is clipboard





    The above script works, but as you can see, I have to redeclare myImg. If I don't, the second time the script writes to the page, I get a js error:myImg is undefined.

    I thought this would be a session variable, for lack of a better term. So why can't I carry the value to the third document without redeclaring it??? Any ideas???



    -james
    Last edited by jamescover; 07-01-2004 at 12:53 PM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    document.writeln('document.writeln("Your image name is ' + myImg + '");');

    I used single quote as delimiter for the outer document.writeln statement to avoid confusion.

    But I don't know exactly what you really wanted to do with that script. I don't know why you're using nested document.writeln statements. Maybe there are better ways of doing it.

  • #3
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    <script>
    <!--
    myImg=new Image()
    myImg.src="http://www.ekigroup.com/javascript/Clipboard.jpg";
    myImg="clipboard";
    function passVar(){
    document.open();
    document.writeln("Your image name is " +myImg);
    document.writeln("<script>");
    document.writeln("<!--");
    document.writeln("function showDoc(){");
    document.writeln("document.open();");
    document.writeln("document.writeln('Your image name is '"+myImg+");");
    document.writeln("document.close();");
    document.writeln("}");
    document.writeln("setTimeout('showDoc()',5000);");
    document.writeln("//-->");
    document.writeln("<\/script>");
    document.close();
    }
    //-->
    </script>
    This should do it.

    //Edit:

    Hm.. I'm somehow too slow.
    Last edited by dumpfi; 07-01-2004 at 01:22 PM.

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're right, Glenn, there should be an easier way, but I still can't get it to work. For some reason, embedding a .js via writeln() still crashes IE. Oddly enough, it works fine offline.

    For example, this works as expected offline:


    <script>
    <!--

    function embedScript(){
    document.open();
    document.writeln("<script type=\"text\/javascript\" language=\"javascript\" src=\"go.js\"><\/script>");
    document.close();
    }

    //-->
    </script>

    <body onload="javascript:embedScript();">


    go.js:

    function go(){
    alert("hello");
    }
    go();


    Anyway, I'm working with 3 documents, created from one script. The simpler way, I suppose, would be to store all variables in an external script, embed it in each document, and access them that way, instead of redeclaring them.
    Unfortunately, I can't do such a simple little thing as that :-(



    The good news is, your syntax worked!!! Thanks. I was escaping everything...never thought of inverting...




    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #5
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, dumpfi, thanks for your input. I appreciate it.

    document.writeln("<script language='javascript' type='text/javascript' src='go.js'></script>");


    Hmmm...I thought I had tried it this way before...it's working now. It's always the little things that take up so much tiime...


    Thanks, again, Glenn!


    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "


  •  

    Posting Permissions

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