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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy document.writeln Not Working As Hoped

    Hello,

    Ok, here is the scenario...

    I have the parent page and in it are 2 frames (but both have pretty much the same functioning so I'll only explain 1).

    Ok, now in one of the frames, a user clicks a link (this is what it looks like):
    Code:
    <a href="javascript:pmSend();">Private Message</a>
    I don't know why, but the forum is putting a space in my javascript command, it should be javascript, not java script:

    The pmSend function is simply:
    Code:
    function pmSend () {
      parent.privateMsg("username");
    }
    I don't know if the above is right because I want the result of privateMsg to be in the parent window, not in the frame window. Please let me know.

    Now in the parent window, the privateMsg function is:
    Code:
    function privateMsg(user) {
      document.writeln("<scr"+"ipt language='JavaScript' src='javascripts.php?chatmain2&user="+user+"' type='text/javascript'>\n");
      document.writeln("<\/scr"+"ipt>\n");
    }
    Now what SHOULD be happening, is whenever the privateMsg function is called, it should print those 2 lines with that username used, and if it's called with a different username, then it should print those 2 lines with the different username, NEVER overwriting the other. And all these lines should be printed in the parent window, not either of the frames.

    But here's what's happening:
    The parent window (meaning the whole page now) is redirecting to the frame which triggered the privateMsg function, and overwrote all other content, and just wrote the 2 specified lines.

    The whole point of this is to dynamically write those 2 lines, and it has to be dynamic because different usernames are called.

    Any help is appreciated, and I thank you ahead of time for any help given.

    Thanks.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    document.write and document.writln only works when the document is loading. If it's finished loading, it will clear the document and open a documetn stream to write to. You'll have to use the DOM instead. See <http://codingforums.com/showthread.p...6335#post80684>, <http://codingforums.com/showthread.p...threadid=16378>, <http://codingforums.com/showthread.p...threadid=11083>
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    document.write will destroy any content on the page since it renders it from scratch.

    You should look into innerHTML to solve your problem.

  • #4
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    Thanks for the fast help.

    For the innerHTML thing, won't that only work for IE?

    I need this to be Netscape and IE compatible.

    Thanks.

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    last time I checked innerHTML is compatible if you use it right....

  • #6
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edited: Hello,

    So in my parent window, if I replace:
    Code:
    function privateMsg(user) {
      document.writeln("<scr"+"ipt language='JavaScript' src='javascripts.php?chatmain2&user="+user+"' type='text/javascript'>\n");
      document.writeln("<\/scr"+"ipt>\n");
    }
    With:
    Code:
    function privateMsg(user) {
      script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = 'javascripts.php?chatmain2&user='+user;
      document.getElementsByTagName('head')[0].apendChild(script);
    }
    And that will output the script calling to the parent window, right? Even though the frame is calling the actual function?

    Thanks.

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Sound's like that, yes.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    Ok, I totally screwed up the code to show you what I'm trying to accomplish (it doesn't work this way).

    Can you please help me fix this?:

    Code:
    function privateMsg(user) {
      eval("script"+user+" = document.createElement('script')");
      eval("script"+user+".type = 'text/javascript'");
      eval("script"+user+".src = 'javascripts.php?chatmain2&user="+user+"'");
      eval("document.getElementsByTagName('head')[0].apendChild(script"+user+")");
    }
    I'm not new to JavaScript, but I'm not very experienced with it either.

    Thanks.

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    function privateMsg(user){
        var
            oScript=document.createElement('script');
        oScript.src='javascripts.php?chatmain2&user='+user;
        oScript.type='text/javascript';
        document.getElementsByTagName('head')[0].appendChild(oScript);
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello,

    liorean, sorry, I should have been more specific.

    I need the variable name to be script and then the value of the variable user passed into the function.

    Example:
    If the value of the user variable was bob, then I need the script variable to be: scriptbob

    Can you help me do that?

    Thanks.

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Oh, but the variable will disappear as soon as the function is finished... the script that you load, however, will be inserted into the document. So, design the script that you load such that it gets the user name from the query you send the php file instead. That's easiest.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The reason I would like this is so that if the same user is called twice (meaning the function is called twice), I don't want the 2 to conflict. Is that a problem, or will it not matter?

  • #13
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried to do it your way and it's not working, nothing is getting printed as far as I can tell.

  • #14
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just inserted some alerts, I know that the privateMsg function is being called and it is functioning all the way through to the end of the function, but still, I don't think the script is actually being printed.

    Please help.

    Thanks.

  • #15
    New Coder
    Join Date
    Oct 2003
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I GOT IT, YOU'RE A GENIUS, THANK YOU SO MUCH!!!!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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