DOMUtils.js (1179B)
1 /** 2 * React Starter Kit (https://www.reactstarterkit.com/) 3 * 4 * Copyright © 2014-present Kriasoft, LLC. All rights reserved. 5 * 6 * This source code is licensed under the MIT license found in the 7 * LICENSE.txt file in the root directory of this source tree. 8 */ 9 10 export function updateTag(tagName, keyName, keyValue, attrName, attrValue) { 11 const node = document.head.querySelector(`${tagName}[${keyName}="${keyValue}"]`); 12 if (node && node.getAttribute(attrName) === attrValue) return; 13 14 // Remove and create a new tag in order to make it work with bookmarks in Safari 15 if (node) { 16 node.parentNode.removeChild(node); 17 } 18 if (typeof attrValue === 'string') { 19 const nextNode = document.createElement(tagName); 20 nextNode.setAttribute(keyName, keyValue); 21 nextNode.setAttribute(attrName, attrValue); 22 document.head.appendChild(nextNode); 23 } 24 } 25 26 export function updateMeta(name, content) { 27 updateTag('meta', 'name', name, 'content', content); 28 } 29 30 export function updateCustomMeta(property, content) { 31 updateTag('meta', 'property', property, 'content', content); 32 } 33 34 export function updateLink(rel, href) { 35 updateTag('link', 'rel', rel, 'href', href); 36 }