Путь - [ 1 ]
function getMetaContent(property, name){
return document.head.querySelector("["+property+"="+name+"]").content;
}
console.log(getMetaContent('name', 'csrf-token'));
Вы можете получить ошибку:
Uncaught TypeError: Невозможно прочитать свойство 'getAttribute' с нулевым значением
Путь - [ 2 ]
function getMetaContent(name){
return document.getElementsByTagName('meta')[name].getAttribute("content");
}
console.log(getMetaContent('csrf-token'));
Вы можете получить ошибку:
Uncaught TypeError: Невозможно прочитать свойство 'getAttribute' с нулевым значением
Путь - [ 3 ]
function getMetaContent(name){
name = document.getElementsByTagName('meta')[name];
if(name != undefined){
name = name.getAttribute("content");
if(name != undefined){
return name;
}
}
return null;
}
console.log(getMetaContent('csrf-token'));
Вместо того, чтобы получить ошибку, вы получите null
, это хорошо.