Obtain the HTML link embedded within the DOM header element resource, without relying on any identifiers such as ID

My website has the following code structure:

<link rel="amphtml" href="http://example.com/?amp"></head>

I want to extract the html link only on pages where the "amphtml" rel value is present. I attempted this code, but it causes issues on URLs where that tag is not found.

var ampLink_txt = document.querySelector("link[rel=amphtml]").getAttribute("href");

Since I can't modify the HTML document by adding any IDs or classes, could someone guide me in the right direction?

Answer №1

.querySelector has the possibility of returning null if it doesn't find anything. To avoid errors, always check if the result is null before proceeding:

const targetElement = document.querySelector(".target-class")

if (targetElement) {
   const targetText = targetElement.textContent
   // Additional operations here...
}

For more information, visit https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dividing Faces with Lengthy Edges in three.js into Two Separate Faces

I am currently working on a script that involves splitting overly long edges of faces into two separate faces instead of one large face. The final result is exported to a .obj file. Although the geometry reduction works fine, I have noticed some issues a ...

Issue with selecting rows in DataTable on subsequent pages

I am currently working on an ASP.NET MVC web application and encountering some issues with a jQuery dataTable. The table is being populated correctly with data from a ViewBag, so that part is working fine. <div> <table id="invoiceTable"> ...

Exploring the various methods of creating controllers and services in AngularJS and understanding the rationale behind each approach

I've been observing various instances of controller and service creation in AngularJS and I'm feeling perplexed. Could someone elucidate the distinctions between these two methods? app.service('reverseService', function() { this.re ...

Guide to switching from test mode to live mode and enabling live mode in stripe with nodejs

I have encountered an issue with the stripe form I am currently using for payments. When the form is loading, it displays "test mode" in the top right corner. I am unsure how to switch it to live mode and cannot find any option on the stripe dashboard to d ...

Updating token using an Ajax request in a PHP webpage

Currently, I am encountering an issue with my token system for requesting PHP pages via Ajax. The problem arises when attempting to make multiple Ajax requests from the same page as I am unable to refresh the token on the initial page. To elaborate furthe ...

Using Node.js to pass the response object to a Bull queue for server-side events

I'm facing a dilemma with an architectural decision. Currently, I have a Node + Express app with an API for file uploads. Once a file is uploaded, it is processed in batches by FFMPEG using Bull Queue + Redis. This setup functions well, but I've ...

Svelte warns of potential undefined Variable when using "bind:this={}"

Whenever I attempt to utilize the bind:this attribute in Svelte, I encounter this message in vscode: 'cardGroup' is possibly 'undefined'.js(18048) Upon execution, the following error arises: TypeError: Cannot read properties of undefin ...

Learn how to display or conceal the HTML for 'Share this' buttons on specific routes defined in the index.html file

Currently, I am in the process of updating an existing Angular application. One of the requirements is to hide the "Share this buttons" on specific routes within the application. The "Share" module typically appears on the left side of the browser window a ...

Problem: Both select lists are moving all items

The code below pertains to a dual select list, as depicted in the image at this link: https://i.sstatic.net/edd21.png It is functioning as intended. The only issue is that when I click on the last subject in the right-hand side list box (select Subject l ...

jquery-validation error in gulp automations

I've encountered a sudden error in my gulp build related to jQuery validation. There haven't been any recent changes that would trigger this issue. Interestingly, one of my colleagues is experiencing the same problem, while another one is not. We ...

How to access a class from another JavaScript file using a function call

The title may seem strange, but I will do my best to explain the situation clearly. I have a website with a navigation bar where each tab corresponds to a different php file. All the files share a common js and css file. The directory structure is as foll ...

What is the process to designate a specific value to a key in an array?

I need assistance in updating the description array within the schema by adding the about and link values, followed by using the .save() function to store it in the database. Any guidance on this issue would be greatly appreciated. Thank you for your help. ...

What is the most efficient way to utilize Promise.all in conjunction with React-Redux-Thunk?

export const FETCH_DB_BEGIN = 'FETCH_DB_BEGIN' export const FETCH_DB_SUCCESS = 'FETCH_DB_SUCCESS' export const FETCH_DB_FAILURE = 'FETCH_DB_FAILURE' export const fetchDatabase = () => { return dispatch => { ...

"Utilizing Vue Mixins on a global scale, but restricting their usage to local components

Is there a way to use a mixin in multiple components without having to constantly import and declare it? I've tried connecting the mixin object to a global variable using vue.prototype, but mixins are added to components before globals are accessible. ...

The Jquery script is ineffective for newly added elements

After creating an Ajax Form that functions well, I noticed that when the result of the form is another form, my script does not work for the new form generated. Is there a way to make the new form function like the old one? $(document).ready(function() ...

Issue with JQuery Ajax button functionality

I am brand new to navigating the world of writing ajax and using a restful api...so please bear with me. On the backend, I have a Laravel 5.2 RESTful API that I'm working with, and my current task involves loading a list of categories using Jquery / ...

Progressive Web App (PWA) default start URL

I am currently facing an issue with my Vue app, which is also a Progressive Web App (PWA). While the PWA functions correctly as planned, I realized that I am using generic paths for my web application. This means that in order to access the correct page, ...

Adjust the language code in a URL using JavaScript or Node.js

Within my common header file, there is a navbar that includes a multilanguage dropdown menu. The issue I am facing is that when I select a language from the dropdown, the page translates correctly. However, when I navigate to other pages, the selected lang ...

React Error: Unable to Call function this.state.Data.map

I'm encountering an issue with mapping objects in ReactJS. Even though I am able to fetch data, when I try to map it, I receive the following error: this.state.Data.map is not a function Here's the code snippet: import React, { Component } fro ...

How to include a date in an Angular date input field

I've simplified some logic in this JSFiddle: http://jsfiddle.net/r7vyjg4h/ and it's functioning correctly. The HTML code is as follows: <div ng-controller="MyCtrl"> <div ng-repeat="line in lines"> <div style="display: inline- ...