Tuesday, 12 April 2016

How to write JavaScript in Blogger Template

How to check for Special Characters in a String in Java

How to put JavaScript in Blogger Template

In Blogger you may sometimes need to use JavaScript to define various functionalities for your website.
But in many cases we run into a XML parse error while trying to include javascript inside our blogger template.
Below are two solutions to this problem.

OPTION 1:Using CDATA for including JavaScript in Blogger Template

Blogger uses XHTML for all its templates.
So if you are writing normal javascript inside the <script> tag then you will end up with a XML parse Error.

So in order to achieve this you need to put your javascript code inside the CDATA tag.
This is achieved in the following manner.

// Put your JavaScript here

So for Example:

      var x = 5;
      var y = 6;
      document.getElementById("demo").innerHTML = x + y;

If the 1st Option DOES NOT Work then try the 2nd.

OPTION 2:Use CDATA - Method 2

Another specification to use this is like this.

<script type="text/javascript">



OPTION 3:Using External JavaScript files.

Another way out of this is to use external JavaScript files.
For this you need to keep your javascript files online somewhere.
If you are hosting your own files, then you can take care of this.
If NOT then you can use many Online Javascript Hosting sites like "YourJavaScript.com"
to host the JavaScript files for you.

Suppose we are including our JavaScripts inside our <head> tag. Then this would look like this.

 <script src='http://yourjavascript.com/10232246114/XYZ123.js' type='text/javascript'/>

No comments:

Post a Comment

Coder Magnet
CoderMagnet is full of resources from our daily development activities. It has solutions for common problematic scenarios in technologies like Java 8, AEM, JCR and also occasionally gives you tips on Blogger as well.