简体   繁体   中英

Get URL Title from External URL without loading whole Website

I have a javascript code which shows all the urls in a textarea / textbox. I would like to show the web title of those urls. But I don't want to load the whole website (get_content) as it manipulates the other javascripts on my page. How can I show the title?

My Code

<script>
let result = $("#converted_url");
$("#textarea_input").on("input", function() {
  result.html("");
  var urlRegex = /(?:(?:http|https):\/\/)?([-a-zA-Z0-9.]{2,256}\.[a-z]{2,4})\b(?:\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
  var found = [];
  $("#textarea_input").val().replace(urlRegex, function(url) {
    let trimmedUrl = url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "");
    if (found.includes(trimmedUrl)) {
      return;
    }
    found.push(trimmedUrl);
    var link = '<a target="_blank" href="' + url + '">' + TITLE + '</a>';
    result.append(link);
  });
});
</script>

Should output something like this:

在此处输入图片说明

Simply put: you can't. The title of a webpage is only stored in its HTML content, so without downloading that content, you can't get the title. I don't understand what you mean by "it manipulates the Javascripts on my page" -- if you make an AJAX call to the URL you want (assuming it's same-domain or has CORS enabled), you'll get back the raw source code as a string to parse as you wish, and it won't affect anything else on your page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM