1

So I am working with some data that gets sent back from the server after a file upload in Javascript. It is sending back a URL, and I need it to appear as a hyperlink and append it to the document. What is the best way to go about doing this? I could do it serverside though that doesn't seem to be very clean, and I googled and found the str.link() method but read it was deprecated. My code in JS:

init: function() {
    this.on("success", function(file, responseText) {
        var span = document.createElement('span'); 
        span.setAttribute("style", "position: absolute; bottom: -28px; left: 3px; height: 28px; line-height: 28px;   "); 
        span.innerHTML = responseText;
        file.previewTemplate.appendChild(span);

    });
}

2 Answers 2

2

Try this:

var a = document.createElement('a'); // Create a new <a>
a.href = responseText;              // Set it's href
a.innerHTML = responseText;        // Set it's text
span.appendChild(a);              // Append it to the span

(Or you can just replace the span with the <a> altogether.)
In that case, you'll obviously have to apply the css to the <a>:

a.setAttribute("style", "position: absolute; bottom: -28px; left: 3px; height: 28px; line-height: 28px;");

However, using a css class would be cleaner:

a.className = "MyCssClass";
Sign up to request clarification or add additional context in comments.

1 Comment

Works good, one question. How can I get the link to open in a new tab instead of the same tab? I know it is a.setattribute(), but what should I set? Thank you.
2

You can create an hyperlink instead of a span :

var link = document.createElement('a');
link.href = responseText;
link.innerHTML = responseText;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.