How to display a html link in container?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlellis
    New Member
    • Mar 2015
    • 24

    How to display a html link in container?

    I have four records in a file and one of them contains a link like <a href = "http://steelers.com">P ittsburgh Steelers</a> win again.

    I am reading the records and storing them in a container called div1 The problem is that I want the Pittsburgh Steelers words above to show up as an actual link when they are added to the div1. Right now the link is showing up just like it is in the file. I am using javascript and innerHTML.

    The four records are appended to a variable called dataIN. Then the last statement is document.getEle mentByID('div1' ).innerhtml += dataIN.

    I want to store Pittsburgh Steelers win again in the container instead of actual text version
    <a href = "http://steelers.com">P ittsburgh Steelers</a> win again
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Works fine for me.
    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    			function test() {
    				document.getElementById('div1').innerHTML += " <a href = 'http://steelers.com'>Pittsburgh Steelers</a> win again";
    			}
    		</script>
    	</head>
    
    	<body onload="test()">
    		<div id="div1">test</div>
    	</body>
    </html>
    There's probably something else in your code that is wrong. You would need to post it.

    Comment

    • jlellis
      New Member
      • Mar 2015
      • 24

      #3
      Yes I can easily do it that way myself. I have done that. The problem is that the <a href = 'http://steelers.com'>P ittsburgh Steelers</a> win again";
      is inside a file.

      Code:
      <script type="text/javascript">
      function LoadFile() {
          var oFrame = document.getElementById("frmFile");
          var strRawContents = oFrame.contentWindow.document.body.childNodes[0].innerHTML;
          while (strRawContents.indexOf("\r") >= 0)
              strRawContents = strRawContents.replace("\r", "");
          var arrLines = strRawContents.split("\n");
          //alert("File " + oFrame.src + " has " + arrLines.length + " lines");
          var html='';
          for (var i = 0; i < arrLines.length; i++) {
              var curLine = arrLines[i];
      
      if(curLine.search("href") > -1)
      {
      var divP1 = '<div>';
      var divP2 = '</div>';
      var tLine = curLine;
      var cat1 = divP1.concat(curLine);
      cat2 = cat1.concat(divP2);
      //curLine = cat2.replace(/</g,'&lt;').replace(/>/g,'&gt;');
      curLine = htmlEscape(curLine);
      document.getElementById('div1').href = curLine;
      //        html += curLine + '<br />';
      //curLine = divP1.concat(tLine);
      //document.getElementById('div1').innerHTML = '<div><a href="http://www.steelers.com">Steelers</a></div>';
      //document.getElementById('div1').innerHTML = cat2;;
      }
              html += curLine + '<br />';
              //alert("Line #" + (i + 1) + " is: '" + curLine + "'");
          }
      //    html = html.replace(/</g,'&lt;').replace(/>/g,'&gt;');
          document.getElementById('div1').innerHTML+= html;
      }
      </script>
      *************** **********
      Input File
      This is line 1.
      This is line 2.
      This is line 3.
      <a href="http://steelers.com">P ittsburgh Steelers</a>
      *************** *************** *************** ********
      Results inside div1
      Same as the 4 lines above.
      Last edited by Rabbit; Mar 23 '15, 07:24 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

      Comment

      • jlellis
        New Member
        • Mar 2015
        • 24

        #4
        Here is the iframe and div

        Code:
        <iframe id="frmFile" src="
        [url]http://mtcsrv1078/sites/BOOST/MainTest/Shared[/url] Documents/demo_test.txt" onload="LoadFile();" style="display: none;"></iframe>
        
            <div class="m2">
         <div id = "div1"></div>
                
        </div>
        
        </div>
        Last edited by Rabbit; Mar 23 '15, 07:24 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

        Comment

        • jlellis
          New Member
          • Mar 2015
          • 24

          #5
          So even though html is a string variable, I am trying to convert incoming strings that contain an href to html and add it to the innerHTML as well as regular strings.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            This line:
            Code:
            curLine = htmlEscape(curLine);
            You are escaping HTML.

            Comment

            • jlellis
              New Member
              • Mar 2015
              • 24

              #7
              Thanks Rabbit for your response. I'll keep playing with it to see what happens. Line 25 above is basically doing the same thing you are doing. I want user to be able to edit the text file at any time and each time an edit is made, the div will update as well with the additional text. So sometimes they may put href or other html tags in the text file.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                No it's not the same thing I am doing. I never escape the HTML.

                If you escape the HTML in my string above, then it would be what you're doing. Escaping HTML takes HTML strings and makes it not HTML.

                Comment

                • jlellis
                  New Member
                  • Mar 2015
                  • 24

                  #9
                  //document.getEle mentById('div1' ).innerHTML = '<div><a href="http://www.steelers.co m">Steelers</a></div>';

                  Comment

                  • jlellis
                    New Member
                    • Mar 2015
                    • 24

                    #10
                    That's the line I am referring to, not the escape line.

                    Comment

                    • jlellis
                      New Member
                      • Mar 2015
                      • 24

                      #11
                      Even without the escape line it still does the same thing. I was just trying something with the escape.

                      Comment

                      • jlellis
                        New Member
                        • Mar 2015
                        • 24

                        #12
                        Even if I try to use replace, it just puts the following in the container.

                        &lt;a href="http://steelers.com"&g t; Pittsburg Steelers&lt;/a&gt;

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          That line is commented out so it doesn't do anything.

                          Comment

                          • jlellis
                            New Member
                            • Mar 2015
                            • 24

                            #14
                            I know that. I'm the one who commented it out. As mentioned I can get it to work that way: the way the commented out line is doing. Once again I am trying to read lines(some containing html tags) from a file and add it to the div.

                            Comment

                            • jlellis
                              New Member
                              • Mar 2015
                              • 24

                              #15
                              Thanks for your help. I'll fix it I'm sure.

                              Comment

                              Working...