Copy link to clipboard
Copied
Can anyone please explain so that even I can understand the reason why the following script does not work as I would expect?
I am trying to append a line of html code to the <head> section of a Page Template.
<script type="text/javascript">
function loadScript() {
if(window.location.href.indexOf("fish") > -1) {
alert("the url contains the string fish");
var csslink = document.createElement("link");
csslink.setAttribute("rel", "canonical");
csslink.setAttribute("type", "text/css");
csslink.setAttribute("href", "http://www.xyz.com/fish/herring");
document.getElementsByTagName("head")[0].appendChild(csslink);
}
window.onload = loadScript();
</script>
Secondly if one cannot append to the <head> can one append to the <body> ?
Any help appreciated.
Kind regards
PW
Copy link to clipboard
Copied
Hello peterwulf,
Looks like you're missing a } above the widow.onload. That should fix it.
Hope this helps,
Chad Smith - BC Gurus