Get a value from <link>

Viewed 52

Friends, I would like to get the value of or just know if it exists, does anyone have an idea?

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
    <link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c" >
    <link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b" >

</head>

<body>

 
    <link extension-installed="0318648714777">


<p>Click the button to find out how many <link> elements.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementsByTagName("link");

  document.getElementById("demo").innerHTML = x.length;
}
</script>
</pre>

unfortunately it counts all page

4 Answers

Use querySelector with the selector string body > link to find a <link> directly inside the body:

function myFunction() {
  const link = document.querySelector('body > link');
  console.log(link);
  console.log(link.getAttribute('extension-installed'));
}
</style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c">
<link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b">

<style>
<link extension-installed="0318648714777">


<p>Click the button to find out how many
  <link> elements.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

If it's not a direct child of the body, use body link instead, to get a link anywhere inside the body.

You can use querySelectorAll("link"). It will give the nodelist containing all the link elements

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
    <link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c" >
    <link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b" >

</head>

<body>


    <link extension-installed="0318648714777">


<p>Click the button to find out how many link elements.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.querySelectorAll("link");

  document.getElementById("demo").innerHTML = x.length;
}
</script>
</pre>

You can try getting it by attribute extension-installed, if its common for all your links.. Something like document.querySelectorAll('[extension-installed]');

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Untitled Document</title>
  <link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c">
  <link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b">

</head>

<body>


  <link extension-installed="0318648714777">


  <p>Click the button to find out how many
    <link> elements.</p>

  <button onclick="myFunction()">Try it</button>

  <p id="demo"></p>

  <script>
    function myFunction() {
      var x = document.querySelectorAll('[extension-installed]');

      document.getElementById("demo").innerHTML = x.length;
    }
  </script>
  </pre>

As you are searching the elements with tag:

  document.getElementsByTagName("link");

They are actually four there:

<link rel="stylesheet" type="text/css" href="stacks.css?v=f956aeec157c"></link>,
<link rel="stylesheet" type="text/css" href="primary.css?v=f68ca0e24c3b"></link>,
<link extension-installed="0318648714777"></link>,
<link></link>

If you need specific link, try using an id with it and select by ID:

<link extension-installed="0318648714777" id="myLink"></link>

var x = document.getElementsById("myLink");

Or simply try to find link with attribute extension-installed

var x = document.querySelectorAll('[extension-installed]');
Related