How to get source code of Input url inside div?

Viewed 37

Here is my HTML and I want to show source code of url with (view-source+url) inside class .source-html from input when I click on button?

const input = document.querySelector("#input");
const button = document.querySelector(".view");

button.addEventListener("click", (x) => {
  const url = input.value;
  const sourceUrl = "view-source:" + url;
  //Some code that show source of url inside div

});
<input id="input" type="text" />
<button type="button" class="view">View Source</button>

<div class="source-html"></div>

If possible, help me.

1 Answers

The modern browsers not allowed to load local source of the website. This is due to security policies. If you load other websites source into your site, then there is a chance to load malicious scripts.

Still if you try to load the view-source then you may encounter with "CORS error" or "Not allowed to load local resource: view-source:"

You can still use the below code to view the source of same origin sites.

const input = document.querySelector("#input");
const button = document.querySelector(".view");

button.addEventListener("click", (x) => {
  const url = input.value;

  //view source of same origin
    $.ajax({
        url: url,
        success: function(x){
            console.log(x);
        }
    })
});
Related