Start · Sprachen · JavaScript · Referenz · MathMLAnchorElement.type

MathMLAnchorElement.type

Eigenschaft

Liest oder setzt den MIME-Typ der verlinkten Ressource eines MathML-<code>&lt;a&gt;</code>-Elements und spiegelt das Attribut <code>type</code> wider.

seit JavaScript Experimentell: nur in neueren Browsern, keine Base Kategorie: webapi

Signatur

MathMLAnchorElement.type

Beschreibung

Die Eigenschaft type der Schnittstelle MathMLAnchorElement ist ein String, der das Inhaltsattribut type eines MathML-Ankers (<a> im MathML-Namensraum http://www.w3.org/1998/Math/MathML) widerspiegelt. Sie gibt den erwarteten MIME-Typ der verlinkten Ressource an, z. B. application/pdf oder text/html.

Der Wert ist ein reiner Hinweis (engl. „advisory“): Der Browser prüft ihn nicht und nutzt ihn weder für die Navigation noch für den Download. Er dient vor allem Hilfstechnologien, Styling über Attributselektoren (z. B. a[type="application/pdf"]) und eigenen Skripten, die Links je nach Zieltyp unterschiedlich behandeln möchten.

Beim Lesen erhält man den aktuellen Attributwert oder einen leeren String, wenn das Attribut fehlt. Beim Setzen wird das Attribut type im DOM angelegt bzw. überschrieben. Die Eigenschaft verhält sich damit analog zu HTMLAnchorElement.type.

Parameter

Name Typ Default Beschreibung
$wert string "" Beim Setzen: ein MIME-Typ wie application/pdf. Wird unverändert in das Attribut type geschrieben; eine Validierung findet nicht statt.

Rückgabewert

Typ
string
Beschreibung
Der Wert des Attributs type oder ein leerer String "", wenn das Attribut nicht gesetzt ist.

Beispiele

MIME-Typ eines vorhandenen MathML-Links auslesen

// HTML: <math><a href="/formeln/euler.pdf" type="application/pdf"><mi>e</mi></a></math>
const MATHML_NS = 'http://www.w3.org/1998/Math/MathML';
const anker = document.getElementsByTagNameNS(MATHML_NS, 'a')[0];

if (typeof MathMLAnchorElement !== 'undefined' && anker instanceof MathMLAnchorElement) {
  console.log(anker.type);
} else {
  // Fallback für Browser ohne MathMLAnchorElement
  console.log(anker.getAttribute('type'));
}
application/pdf

MathML-Link dynamisch erzeugen und Typ setzen

const MATHML_NS = 'http://www.w3.org/1998/Math/MathML';
const math = document.createElementNS(MATHML_NS, 'math');
const link = document.createElementNS(MATHML_NS, 'a');
const mi = document.createElementNS(MATHML_NS, 'mi');
mi.textContent = 'π';

link.setAttribute('href', '/doku/pi.html');
if ('type' in link) {
  link.type = 'text/html';
} else {
  link.setAttribute('type', 'text/html');
}

link.append(mi);
math.append(link);
document.body.append(math);

console.log(link.getAttribute('type'));
text/html

Links auf PDF-Dokumente kennzeichnen

const MATHML_NS = 'http://www.w3.org/1998/Math/MathML';
const anker = [...document.getElementsByTagNameNS(MATHML_NS, 'a')];

const pdfLinks = anker.filter((a) => (a.type ?? a.getAttribute('type')) === 'application/pdf');

pdfLinks.forEach((a) => {
  a.setAttribute('aria-description', 'Öffnet ein PDF-Dokument');
});

console.log(`${pdfLinks.length} PDF-Link(s) markiert`);
2 PDF-Link(s) markiert

// Wichtig · Fallstricke

  • Kompatibilität: MathMLAnchorElement ist neu und experimentell. In Browsern ohne Unterstützung liefert createElementNS(MATHML_NS, 'a') nur ein allgemeines MathMLElement oder Element, dann ist type undefined. Vorher per typeof MathMLAnchorElement !== 'undefined' oder 'type' in element prüfen und notfalls getAttribute('type') verwenden.
  • Nur ein Hinweis: Der Wert wird nicht geprüft. Der tatsächliche Typ ergibt sich aus dem Content-Type-Header des Servers. Für Sicherheitsentscheidungen darf man sich deshalb nicht auf type verlassen.
  • Namensraum beachten: Ein <a>, das mit document.createElement('a') erzeugt wird, ist ein HTMLAnchorElement. Nur mit createElementNS und dem MathML-Namensraum oder innerhalb von <math> im HTML-Parser entsteht ein MathML-Anker.
  • Es gibt keine Normalisierung: Groß- und Kleinschreibung sowie Parameter wie ; charset=utf-8 bleiben unverändert erhalten.