最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Converting URL into clickable link using Javascript - Stack Overflow

programmeradmin4浏览0评论

I have a text field called 'patentURL' in a form. The user enteres the plete 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?

I have a text field called 'patentURL' in a form. The user enteres the plete 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?

Share Improve this question asked May 9, 2012 at 7:48 PrmkPrmk 1752 gold badges2 silver badges13 bronze badges 1
  • Does this answer your question? Use Javascript to get url of the page and use it in a link – Nikola Lukic Commented Dec 9, 2019 at 12:26
Add a ment  | 

4 Answers 4

Reset to default 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

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

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./webtricks/convert-url-text-into-clickable-html-links-using-javascript.php

Example to call href in Javascript:

function call_link() {
    location.href = 'www.google.';
}
发布评论

评论列表(0)

  1. 暂无评论