SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Randomizing document.write()

    This may sound like a little bit on a crazy side, but there is a good intention for this.

    I need to make a javascript function that can be called via the <body onload="javascript:blah()">. Basically what the job of the function is... it should scan the <body> section of the page for certain predefined delimiters, for simplicity's sake let's name them <!--begin-weird-js--> and <!--end-weird-js-->. Then the js should send a document.write() to a random place between those delimeters. The only catch is that the random place should not be withing another html tag. So this example:

    <html>
    <head>
    <title>PageABC</page>
    <script src="weird.js" />
    </head>
    <body onload="javascript:weird();">
    <h2>Site.com - Page</h2>
    <!--begin-weird-js-->
    [space]<p>asdasdksajdksajdksajdksajdkasjdksjksdajsdkja</p>[space]
    <p>dasddddd<a href="asa">asa</a>asasas</p>[space]
    <!--end-weird-js-->
    <p>Copyright 2002 Site.com</p>
    </body>
    </html>
    In the above example the script should use the document.write() only in one of the places in bold.

    So any ideas how to accomplish something like this? Sorry if it sounds a tad difficult and weird but I would greatly appreciate any help.

  2. #2
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does indeed sound completely mad, but I'm guessing that you want to insert advertising in this way to ensure that they don't filter it out... is this right?

    If so, you should be aware that it could easily be bypassed by blocking the URL of the JS file, or stripping the onLoad event from the body tag (or perhaps just the "weird();" bit).

    Anyway, AFAIK comment blocks cannot be retrieved from the DOM, and so I suggest you use DIVs with IDs that you have generated, which you can then get a reference to from the DOM. Once you have this, you can (in IE5+ and possibly NS6+) move through the child elements of this element (in IE, you would use the .childNodes property/collection), allowing you insert your tag/code at will between two child elements or indeed as the first or last child itself. This approach would allow you to replicate the example you give.

    PS - using the "javascript:" prefix is entirely unnecessary for events, it is only used for URLs, and even then it is usually better approached another way.
    Last edited by M@rco; Dec 10, 2002 at 18:54.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  3. #3
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually tha'ts not the reason.

    I'm working on a nice rpg systems for my forums. One of the elements is users can find random items on the site and the forums. So I thought it might be a good way to randomize where to insert "you found an item, click to claim" image to make it a tad more fun.

    What the "special" tags are totally irllevant. Could be comments, divs, etc. So divs will work fine. Any ideas how to make the document.write() to do it's job in a random area though?
    Last edited by Codename49; Dec 10, 2002 at 20:36.

  4. #4
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To insert some HTML after an element use:
    Code:
    element.outerHTML += HTMLCode;
    and to insert before, use:
    Code:
    element.outerHTML = HTMLCode + element.outerHTML;
    This is IE-only, but I'm sure that you can do something similar with NS6+ if you read the docs!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Originally posted by M@rco
    To insert some HTML after an element use:
    Code:
    element.outerHTML += HTMLCode;
    and to insert before, use:
    Code:
    element.outerHTML = HTMLCode + element.outerHTML;
    This is IE-only, but I'm sure that you can do something similar with NS6+ if you read the docs!
    I know innerHTML is supported in Netscape now (trying to at least be somewhat compatible with IE, but it's not part of any DOM spec), so I would believe that outerHTML is also supported. However, this will not work in other browsers such as Opera and Konqueror, so know your audience!

    --Vinnie


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
  •