Tibco está eliminando JQuery de Spotfire y tengo algunos paneles que dependen de Jquery para funcionar, como se muestra a continuación:
var css = "https://localhost/css/style.css";
if (!$('link[href="' + css +'"]').length) {
$("<link/>", { "rel": "stylesheet", "type": "text/css", "href": css }).appendTo("head");
}
Me gustaría hacer lo mismo usando Javascript, pero no pude encontrar una solución que me permitiera cargar el CSS solo si no existe en DOM.
Cualquier selector que funcione en jQuery funcionará con el método document.querySelector .
Algo como esto:
if (!document.querySelector(`link[href='${css}']`)) {
document.head.innerHTML += `<link rel="stylesheet" href="${css}" type="text/css"/>`;
}
const cssHref = "https://localhost/css/style.css";
// If element not found returns `null`
const stylesheet = document.querySelector(`link[href="${cssHref}"]`)
if (!stylesheet) {
// You can create your own helper function to simplify the creation of the element
// instead of manually creating the <link> element
const style = document.createElement('link')
style.rel = 'stylesheet'
style.href = cssHref
document.head.append(style)
}