Insert code only link into textarea

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roamer
    New Member
    • Dec 2012
    • 23

    Insert code only link into textarea

    This should be very simple.
    It is NOT a live link!
    I have no idea how to do this, so need some help please.

    Code:
    <form>
    <input type="button" id="link" value="Link"><br>
    <textarea name="message" style="width:40%; height:100px;">
    <a href="http://domain.com">Name</a>
    </textarea><br>
    <input type="submit" value="Save">
    </form>
    The object is to:
    1 Click the Link button.
    2 Enter URL into 1st prompt
    3 Enter Name into 2nd prompt
    4 The HTML will show in the textarea (like above)
    5 Save does all the rest elsewhere.

    I reckon this will probably not be more than 1k of JavaScript.

    Hope someone can help - thanks in advance.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    In your scenario, I think point 2 and 3 should go before point 1. Its' much easier to first input the data you need, and by clicking on the link start to do something with that data.

    About point 4: I think it's not possible to show HTML of an external site in a textarea.

    You should use IFRAME for that purpose.

    Comment

    • Roamer
      New Member
      • Dec 2012
      • 23

      #3
      Thanks

      HTML in a textarea is standard for editors. The code gets parsed when processed.

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        OK, I was bored, so I googled for the answer I did not know, and hers is the output:

        Code:
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <script>
        
                // https://stackoverflow.com/questions/10642289/return-html-content-as-a-string-given-url-javascript-function
                function httpGet(theUrl) {
                    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                        xmlhttp = new XMLHttpRequest();
                    }
                    else {// code for IE6, IE5
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange = function () {
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                            document.getElementById('returnText').value = xmlhttp.responseText;
                            //returnText.value = "TESTTEXT";
                            //return "TESTTEXT";
                            //return xmlhttp.responseText;
                        }
                    }
                    xmlhttp.open("GET", theUrl, false);
                    xmlhttp.send();
                }
        
        
                function dostuff() {
                    var link = document.getElementById('textlink');
                    alert('dostuff:' + link.value);
                    httpGet(link.value);
        
                }
        
            </script>
        </head>
        <body>
            <input type="button" id="link" value="Link" onclick="dostuff()"><br>
        <textarea id='textlink'>http://domain.com</textarea>
            <form onsubmit="dostuff()">
                <input type="button" id="link" value="Link" onsubmit="dostuff()"><br>
        <textarea name="message" id="returnText" style="width:40%; height:100px;">
        <a href="http://domain.com">Name</a>
        <h1>TEST-H1</h1>
        </textarea><br>
                <input type="submit" value="Save">
            </form>
            <p>You might get an error described on this page: https://techsupport.osisoft.com/Documentation/PI-Web-API/help/topics/cross-origin-resource-sharing.html</p>
        </body>
        </html>

        Comment

        • CaseyJohnes
          New Member
          • Nov 2018
          • 1

          #5
          Can you do it vice versa? Maybe it will help?

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            "vice versa"?, you mean putting the (html-)code back on the server?

            That will be pretty useless. A properly configured server will not allow that (to be done from a remote location).

            Also a lot of servers use logic to create HTML-pages, like PHP, NodeJS, .ASP . If that's the case, than it will be even more useless to put code back to the server.

            Comment

            Working...