-1

I am trying to pull out the data with src=""; and load it into variable but with no success.

Here is the code i have been working with:

var JSONObject = document.getElementById("data").innerHTML;
console.log(JSONObject);
<script type='text/javascript' src='https://www.instagram.com/xsolvesoftware/media/' id="data"></script>
AESTHETICS
  • 989
  • 2
  • 14
  • 32

1 Answers1

2

innerHTML means exactly what it says: the inner HTML. It is the HTML between <script> and </script> of which there isn't any.

If you want to read the JS then you need to getAttribute('src') and then make an HTTP request for it (e.g. with fetch or XMLHttpRequest).

The Same Origin Policy will probably block this.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • How to do this? – AESTHETICS Aug 03 '17 at 09:14
  • @AESTHETICS — You don't. See the last line of the answer. (Otherwise you shouldn't have any trouble finding documentation on `getAttribute` and `XMLHttpRequest`. They aren't obscure). – Quentin Aug 03 '17 at 09:17
  • XMLHttpRequest is getting blocked and getAttribute is giving me only link itself to the page which is from src='https://www.instagram.com/xsolvesoftware/media/' using document.getElementById("data").getAttribute('src') to print https://www.instagram.com/xsolvesoftware/media/ with console.log(); – AESTHETICS Aug 03 '17 at 09:22
  • "XMLHttpRequest is getting blocked" — See the last line of the answer. I said that would probably happen. – Quentin Aug 03 '17 at 09:23
  • "getAttribute is giving me only link itself to the page" — Obviously. I said *and* not *or*. `getAttribute` is how you know what URL to pass to XMLHttpRequest (but as I said, the Same Origin Policy blocks your access to that URL). – Quentin Aug 03 '17 at 09:23
  • @AESTHETICS — What you want is impossible to achieve with client side code. That's the best answer you are going to get. – Quentin Aug 03 '17 at 09:25
  • I am pretty sure it is possible. – AESTHETICS Aug 03 '17 at 09:26
  • @AESTHETICS — [The usual limitations and work arounds apply](https://stackoverflow.com/a/35553666/19068) – Quentin Aug 03 '17 at 09:27