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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    does document.write() terminate active functions?

    An example to highlight the problem:

    I run this function onload in body, so it starts when page is loaded:

    function print_alert(){
    alert("this message pops every 10th second");
    setTimeout("print_alert()", 10000);
    }

    This works fine so far, i get an alert every 10 seconds. Next is the problem:

    var s = "some html";
    document.write(s);
    document.close();

    After this code is executed the function print_alert() stops working (like the thread is dead). Why is this so ? Is it possible to let the function still be active after a document.write() ?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    document.write doesn't interrupt anything. Unloading the document does, however. Loading another document means the current document will be unloaded.

    If document.write is used after the document parsing has ended or the document stream has otherwise been closed, it will load the code as a new document instead.
    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
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I make a script to prevent users of an evaluation version of a CMS that is under development to remove a textbox on top of the page. I print the textbox several times pr second to be sure it is not removed on the evaluation version. But if some user uses the write function to overwrite the page after filtering out the tekstbox the function stops running (because of the new document). I still want the function to push out new textboxes, any suggestions ?

  • #4
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there is an event named 'onunload' that fires when a document is unloaded (by ex. write()). Is it it possible to pick up the new document before the old is discarded to do some checks, and if the new document is denied, is it possible to terminate the document unload ?

  • #5
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Hmmm... I'm not quite sure I know what you mean. Depending on what you mean, here are two solutions: When you use the document.write() function it replaces the current document with whatever you tell it to. If you go to view source on a document after the write() function has been used you'll see that write()'s value is all that's left of it.
    SOLUTION 1:
    Have you tried just making the computer write out another script for generating the text box and printing it along with the text box? That wayafter the text box is printed, the script will still be there. Ex:
    Code:
    <script language="JavaScript">
    function checktextbox {
    if (textboxisthere=="0") {
    document.write( variable_holding_text_box );
    }
    window.setTimeout(checktextbox(), 10000);
    }
    </script>
    could instead be written as:
    Code:
    <script language="JavaScript">
    function checktextbox {
    if (textboxisthere=="0") {
    document.write( variable_holding_text_box + variable holding a copy of this script );
    }
    window.setTimeout(checktextbox(), 10000);
    }
    </script>
    SOLUTION 2:
    I really don't like that function except maybe for writing to newly opened windows. Why don't you try changing the innerHTML instead? Ex:
    Code:
    <div name="adiv" id="adiv"></div>
    
    <script language="JavaScript">
    function rediv() {
    alert("This thingy will popup every 10th second, no matter what the div changes to.");
    adiv.innerHTML = "Whatever you wanted to put here. Even the hole body of the page if you want. This script that changes it will remain untouched.";
    window.setTimeout(rediv(), 10000);
    }
    </script>
    Call the script with onLoad to start it and it loops forever without interupting itself by accident. You can even store the entire page you were talking about as a variable and search it for the text box with a separate script to make sure it's there before printing the page again. If it isn't there you can just add it in.

  • #6
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    @oivind

    You can't do this with any degree of certainty. I can easily redfine what your function does. I can easily change the definition of your recursive function and that will end it right there. No need for me to use document.write

  • #7
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes this problem is giving me a headache =) but i think it will be good enough for most users. Some will try to hack it, but it still need some skills, that's something I got to accept...

    The alternative is to deny any javascripts witout my own..

    How do you redefine a function ?

  • #8
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    just define it twice

  • #9
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    I don't think you need to give users the ability to put in javascripts. I don't know many places that do allow it, precisely for this problem. There's a lot worse they can do, ya know. They can rework the entire document structure, they can import porn or copyrighted materials, they can spin infinite loops to crash a browser or render it unusable, they can even try some fishing techniques, using XMLHTTPRequests to phone home to a collection server.

    Giving people the arbitrary ability to code their own crap on a system you're responsible for is BAD news, and since there's no way to define what scripts can and cannot be allowed, there really is no way to prevent them from doing anything. So either accept the chaos that will come for everything and don't waste your time coding futile hurdles, or don't give them javascript if you want to prevent the chaos.

  • #10
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since the CMS will be distributed as encrypted php code, i though of just importing one default empty javascript onload that the CMS clients could define as they want, and i have the possibility to search through that javascript before it is loaded, every other javascript or code will be removed before the page is sendt from server to browser (ex document.write(...)<--remove)

    I don't think any CMS client want's to import porn on their own CMS webpage =) But they want to remove the textbox that says it is an evaluation version.

    But with that function redefinition really makes my solution even worse =)

    Maybe I have to deny all javascripts except my own...


  •  

    Posting Permissions

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