modbender
modbender

Reputation: 419

Correct Usage Of External JavaScript in Blogger

I have created a javascript file in github

The code in the js file is as follows :

function youTube(a,b,c,d)
{
 return ("<iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+a+"' width='210'/><iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+b+"' width='210'/><iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+c+"' width='210'/><iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+d+"' width='210'/>");
}

I used rawgit.com to create a CDN link and put it in script source(src)
And in Blogger>>Edit HTML, at the appropriate place where I wanted it I put the code :

<script src='https://cdn.rawgit.com/example/CTGod/1bc76937/youtubebar.js' type='text/javascript'>
var a="zK5zn44YzKA";
var b="3d21j6xOejU";
var c="U8gD0XiOOL4";
var d="cLnR-L6fNqs";
document.write(youTube(a,b,c,d));
</script> 

When I did not use the code that I have right now put in an external js file and used it normally it worked. The code looked like the below :

<script type='text/javascript'>
var a="zK5zn44YzKA";
var b="3d21j6xOejU";
var c="U8gD0XiOOL4";
var d="cLnR-L6fNqs";
function youTube(a,b,c,d)
{
  return ("<iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+a+"' width='210'/><iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+b+"' width='210'/><iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+c+"' width='210'/><iframe allowfullscreen='1' frameborder='0' height='140' src='https://www.youtube.com/embed/"+d+"' width='210'/>");
}

document.write(youTube(a,b,c,d));
</script>

Now, I want to know why the code ,which when put in external js file doesn't work.
If I need to change my source of js file tell me where to upload and how to use it the best way.

Upvotes: 1

Views: 335

Answers (1)

user6144987
user6144987

Reputation:

You have to use a separate script tag to render external js file like the following

<script src='https://cdn.rawgit.com/yashas123/CTGod/1bc76937/youtubebar.js' type='text/javascript'/>

<script type='text/javascript'>
  var a="zK5zn44YzKA";
  var b="3d21j6xOejU";
  var c="U8gD0XiOOL4";
  var d="cLnR-L6fNqs";
  document.write(youTube(a,b,c,d));
</script> 

Upvotes: 2

Related Questions