document.write alternatives

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marks
    New Member
    • Dec 2007
    • 21

    document.write alternatives

    I have a Javascript code which is in a .js file, I'm aware that document.write doesn't work with XHTML/Firefox however does in I.E

    Is there an alternative that does work?
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by Marks
    I have a Javascript code which is in a .js file, I'm aware that document.write doesn't work with XHTML/Firefox however does in I.E

    Is there an alternative that does work?
    Paste your code here.

    Debasis Jana

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Originally posted by Marks
      I have a Javascript code which is in a .js file, I'm aware that document.write doesn't work with XHTML/Firefox however does in I.E

      Is there an alternative that does work?
      Do you mean an alternative for XHTML? document.write does work in normal HTML in probably all browsers.

      Comment

      • Marks
        New Member
        • Dec 2007
        • 21

        #4
        How about another way for it to work with XHTML? =)

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Use the DOM methods, e.g. document.create Element() and set the element's attributes using setAttribute, for example.

          Comment

          • Marks
            New Member
            • Dec 2007
            • 21

            #6
            Originally posted by acoder
            Use the DOM methods, e.g. document.create Element() and set the element's attributes using setAttribute, for example.
            Here's my code how would I go about changing it?

            Code:
            rnd.today=new Date();
            rnd.seed=rnd.today.getTime();
            
            function rnd() {
                    rnd.seed = (rnd.seed*9301+49297) % 233280;
                    return rnd.seed/(233280.0);
            };
            
            function rand(number) {
            	var result = Math.ceil(rnd()*number);
            	if (!result)result++;
                    return result
            };
            var ad_cnt2 = 3;
            var ad2 = rand(ad_cnt2);
            var link2;
            var adBanner2;
            var width2
            var height2
            if (ad2==1) {
            link2="http://www.domain.com";
            adBanner2="http://www.link2.com/banner.gif";
            width2="468";
            height2="60";
            alt2="Title";
            }
            document.write('<a href="' + link2 + '" target="_blank">');
            document.write('<img class="banner" src="' + adBanner2 + '" width=' + width2 + ' height=' + height2 + ' border=0 alt="' + alt2 + '"></a>');

            Comment

            • kunal pawar
              Contributor
              • Oct 2007
              • 297

              #7
              There is alternative way for document.write is assign output to DIV tag
              like
              document.getEle mentById("your div id").innerHTM L = "your text";

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by Marks
                Here's my code how would I go about changing it?

                Code:
                ...
                document.write('<a href="' + link2 + '" target="_blank">');
                document.write('<img class="banner" src="' + adBanner2 + '" width=' + width2 + ' height=' + height2 + ' border=0 alt="' + alt2 + '"></a>');
                Create the element and then set its attributes:
                [code=javascript]elem = document.create Element("a");
                elem.href = link2;
                elem.target = "_blank";
                document.body.a ppendChild(elem );[/code]and likewise for the second statement.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by kunal pawar
                  There is alternative way for document.write is assign output to DIV tag
                  like
                  document.getEle mentById("your div id").innerHTM L = "your text";
                  I don't think innerHTML is supported properly yet for XHTML in all browsers.

                  Comment

                  Working...