0

I have a text field called 'patentURL' in a form. The user enteres the complete URL into this field while saving the record. When the user searches for this record, the entered URL should be clickable. i.e. in the result page, the entered URL should be clickable.

How do I achieve this using Javascript?

1

4 Answers 4

2

There is a non-standard function, but widely spread - link() MDC

function makeClickable(url) {
    return String.prototype.link ? url.link(url) : '<a href="'+url+'">'+url+'</a>';
}

function makeDOMClickable(url) {
    var link = document.createElement('a');
    link.href = url;
    link.innerHTML = url;
    return link;
}

var url = "http://localhost";
document.write ( makeClickable ( url ) );
document.body.appendChild ( makeDOMClickable ( url ) );

demo

Sign up to request clarification or add additional context in comments.

Comments

1

If i understood correctly you should put the url in a link:

<a href = "URL_ENTERED">URL_ENTERED</a>

With javascript:

var link = document.createElement('a');//create link
link.setAttribute('href', 'URL_ENTERED');//set href
link.innerHTML = 'URL_ENTERED';//set text to be seen
document.body.appendChild(link);//add to body

1 Comment

How do I use this 'href' in javascript?
1

You can use javascript regular expression to achieve this have a look

function convert()
{
  var text=document.getElementById("url").value;
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  var text1=text.replace(exp, "<a href='$1'>$1</a>");
  var exp2 =/(^|[^\/])(www\.[\S]+(\b|$))/gim;
  document.getElementById("converted_url").innerHTML=text1.replace(exp2, '$1<a target="_blank" href="http://$2">$2</a>');
}

this way you can convert any text into link you can find more detail here http://talkerscode.com/webtricks/convert-url-text-into-clickable-html-links-using-javascript.php

Comments

1

Example to call href in Javascript:

function call_link() {
    location.href = 'www.google.com';
}

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.