Web Analytics Made Easy -
StatCounter Showing HTML code of selection - CodingForum

Announcement

Collapse
No announcement yet.

Showing HTML code of selection

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Showing HTML code of selection

    I am trying to do a javascript where the user selects text or an object on the same webpage and clicks a button to display the html code on this selection.

    Is this possible?

    Any help would be most appreciated.

    Thanx in advanced

    Many Regards,
    Ben Chivers

  • #2
    Mozilla 1.1 supports this nativaly,just right click and select "View selection source".

    Comment


    • #3
      i'm pretty sure that's not possible using javascript. if it is, it's almost definitely not possible cross browser.
      bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

      i am a loser geek, crazy with an evil streak,
      yes i do believe there is a violent thing inside of me.

      Comment


      • #4
        Maybe it is possible for IE and Moz using ranges,but it's useless since Moz already supports it.

        Comment


        • #5
          less than one percent of the internet world uses mozilla. on the other hand, better than 50% uses Explorer. Even if this built in functionality, were a part of Explorer, meaning that better 50% of the people, i still wouldn't just let it be, if i wanted it, because i want all of my users to have it. also, what if Ben wants to do something with it, that's not part of what's built in?
          bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

          i am a loser geek, crazy with an evil streak,
          yes i do believe there is a violent thing inside of me.

          Comment


          • #6
            Just curious,

            what would be the purpose of such feature.... maybe there are other ways to do what you need.....
            Vladdy | KL
            "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

            Comment


            • #7
              I've used the following code but it just displays the selected text, not the actual html code. Heres the code:

              <html>
              <head>
              <title>New Page 1</title>
              <script>
              function runscript(){
              var sel = document.selection;
              var rng = sel.createRange();
              alert(rng.text);
              }
              </script>
              </head>
              <body>
              <p><i><b>Some Sample Text</b></i></p>
              <p><input type="button" value="Button" name="B3" onclick="runscript()"></p>
              </body>
              </html>

              I was able to display the documents html code, but I didn't want to do that, I just wanted to display the html code of the selection. Here's the code I used for that:

              <html>
              <head>
              <title>New Page 1</title>
              <script>
              function runscript(){
              iHTML = document.body.innerHTML;
              alert(iHTML);
              var sel = document.selection;
              var rng = sel.createRange();
              alert(rng.text);
              }
              </script>
              </head>
              <body>
              <p><i><b>Some Sample Text</b></i></p>
              <p><input type="button" value="Button" name="B3" onclick="runscript()"></p>
              </body>
              </html>

              Many Regards,
              Ben Chivers

              Comment

              Working...
              X