The CreatorCon Call for Content is officially open! Get started here.

How to add javascript to create UI pages

lrossy
Kilo Contributor

Hello there to all of you,

I am trying to create a bit more dynamic UI page for my application and will like to use some javascript for it.   I have search the wiki and google for ideas on how to add the javascript code like using the UI Script include and have not had success.   Here is a sample code that I want to use and will like to know where I need to add the javascript code to make the UI page work.

The way I am building this homepage is using a single dynamic block.

I will greatly appreciate any input to get this to work.

Here is the code the java code is bold:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300);

@-webkit-keyframes fadeIn {

  from {

      opacity: 0;

  }

  to {

      opacity: 1;

  }

}

@keyframes fadeIn {

  from {

      opacity: 0;

  }

  to {

      opacity: 1;

  }

}

*, *:before, *:after {

  box-sizing: border-box;

  margin: 0;

  padding: 0;

}

html, body, main {

  height: 100%;

  width: 100%;

  color: white;

  font-size: 18px;

  font-weight: 400;

  font-family: 'Open Sans', 'Helvetica Neue', 'Helvetica', sans-serif;

  background-color: #008C9E;

}

header {

  position: absolute;

  top: 0;

  left: 0;

  width: 60%;

  text-align: center;

  opacity: 0;

  -webkit-animation: fadeIn ease-in 1;

                  animation: fadeIn ease-in 1;

  -webkit-animation-fill-mode: forwards;

                  animation-fill-mode: forwards;

  -webkit-animation-duration: .5s;

                  animation-duration: .5s;

}

header .title {

  text-transform: uppercase;

  font-size: 1.2em;

  border: 5px solid white;

  padding: 1em;

  opacity: 0;

  -webkit-animation: fadeIn ease-in 1;

                  animation: fadeIn ease-in 1;

  -webkit-animation-fill-mode: forwards;

                  animation-fill-mode: forwards;

  -webkit-animation-duration: .5s;

                  animation-duration: .5s;

  -webkit-animation-delay: 1s;

                  animation-delay: 1s;

}

nav {

  position: absolute;

  top: 0;

  right: 0;

  width: 40%;

  overflow-y: scroll;

  background-color: #F6F6F6;

}

nav ul li {

  opacity: 0;

  -webkit-transition: .5s opacity;

  transition: .5s opacity;

}

nav ul li.visible {

  opacity: 1;

}

nav li a {

  font-weight: 400;

  display: block;

  text-decoration: none;

  padding: 2em;

  border-bottom: 1px solid #D8D8D8;

  color: #343838;

  -webkit-transition: 0.2s ease;

  transition: 0.2s ease;

}

nav li a span {

  font-weight: 300;

  display: block;

  font-size: .7em;

  color: #D8D8D8;

  font-style: italic;

}

nav li a:hover {

  background-color: #005F6B;

  color: white;

}

nav footer {

  color: #343838;

  text-align: center;

  padding: 1em;

  font-size: 0.7em;

}

@media all and (max-width: 791px) {

  header {

      position: inherit;

      width: 100%;

      top: none;

      left: none;

  }

  header .title {

      border: none;

      padding: 4em;

  }

  nav {

      position: inherit;

      width: 100%;

      top: none;

      right: none;

  }

}

<main>

  <header class="billboard">

      <h1 class="title">Testing</h1>

  </header>

  <nav class="slide-nav">

      <ul>

          <li><a href="#">Home <span>Oh you like home?</span> </a></li>

          <li><a href="#">About <span>Lets talk about it.</span> </a></li>

          <li><a href="#">Clients <span>No one likes clients.</span> </a></li>

          <li><a href="#">Contact Us <span>But please don't.</span> </a></li>

          <li><a href="#">Portfolio <span>No one does that anymore?</span> </a></li>

          <li><a href="#">Haircuts <span>Do I need to get one?</span> </a></li>

          <li><a href="#">Cars <span>You can drive one.</span> </a></li>

          <li><a href="#">Owls <span>Are terrorifying.</span> </a></li>

      </ul>

      <footer>&copy; 2014 mattladner.com</footer>

  </nav>

</main>

javascript

//That junt needs to happen on load and window resize

$(window).on("resize", function () {

     

      //Measure some responsiveness

      var responsive_viewport = $(window).width();

      // if is larger than 791px

      if (responsive_viewport > 791) {

         

          //Let's make some stuff fullscreen

          $('.billboard, .slide-nav').css('height', window.innerHeight);

         

          //Let's center up that h1 and do some measuering

          jQuery.fn.center = function () {

              this.parent().css("position","absolute");

              var t = this.parent().css("top");

              var l = this.parent().css("left");

              this.css("position","absolute");

              this.css("top", ((this.parent().height() - this.outerHeight()) / 2) + this.parent().scrollTop() + "px");

              this.css("left", ((this.parent().width() - this.outerWidth()) / 2) + this.parent().scrollLeft() + "px");

              return this;

          }

          //Center that junt

          $("h1").center();

         

      }

 

}).resize(); //fire off that resize event cause desingers always be stretching windows and stuff

//Load those nav li's sequrntially for purdyness

window.onload = function() {

  var cars = document.querySelectorAll(".slide-nav ul li"), i = 1;

      Array.prototype.forEach.call(cars, function(car) {

      setTimeout(function(){ car.classList.add("visible") }, 100*i)

      i++;

  })

};

1 ACCEPTED SOLUTION

Hi Luis,



The code looks perfect. Just put jquery tag also:


<script>https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>


Screenshot attached.


Hope it helps.


View solution in original post

19 REPLIES 19

Hello Venkat,



Originally you mention to do a UI script and now you said to copy the macro code.   Forgive my ignorance but I am getting to know how to develop in ServiceNow and specially getting java to work on it.   I will appreciate if you can clarify what is it that I need to create to get this to work.   Here is the UI Script that I created per your suggestion.  


find_real_file.png



Pretty much I grabbed all the javascript and added into the UI Script and the API named is test3_custom.   I l left the CSS and the HTML code alone as it shows on previous comment I made.   The only thing I did was to remove all the javascript from the main code in the dynamic block and added the script tag <script> language="javacript" src="scripts/testpage_3custom.jsdbx?v=${gs.getProperty('glide.builddate')}"</script> Let me know what I am not adding to this code.   I will really appreciate it.


Hi Luis,



Thanks for the screenshot of UI script file. That one looks good. I dont see the active check box in screenshot but i believe you would have checked it. But before I go into the script further, I did notice that the added script tag is broken. I have corrected it below. The opening script tag was immediately closing and the attributes were outside hence it would not work properly. We would have to add the attributes as a part of opening tag.



<script></script>



Please let me know what error else you are facing after the change above.


Hello Venkat,



Thanks again for the help. I am still not able to see the page as it should.   I corrected the open and close brackets


(......


      <footer>


      2014 mattladner.com

          </footer>


 


     


<script></script>;


   


"</j:jelly>")



and made sure that the name of the UI script is the same as the tag in the main code in the dynamic block.



After that I go to the Content Management Pages and created a new page and added the dynamic block content and still cannot see the page as Akhil showed.


See screenshot.   Again I appreciate your help.



find_real_file.png


Hi Luis,



Can you remove the semicolon after the script tag? Though it wouldnt stop the page from rendering you would see the semicolon while rendering.



Also, to test if the JS file got loaded..can you test this url in a new tab within your browser replacing the instance in the below URL with your instance name.



https://instance.service-now.com/scripts/testpage3_custom.js



If you see your script file getting downloaded to computer then the the above script tag should work and we can start debugging from there. If not we need to check if whether the source tag of scripts tag above i.e. scripts/testpage3_custom.jsdbx is correct. It is a small test above to check the file resource.



We add jsdbx extension for accessing a UI script from macro and that is why when we access the JS file above in macro we access it as testpage3_custom.jsdbx.


Hello Venkat ,


I tried your suggestion, using the link to see if the script got downloaded, instead I got a 404 error page.   I checked the UI Script and all looks good after removing the semi colon after the script bracket. Also checked the main code in the dynamic block and all looks good to me. Here is the main code that I typed in the dynamic block:



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


      <style>


           


@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300);


@-webkit-keyframes fadeIn {


  from {


      opacity: 0;


  }


  to {


      opacity: 1;


  }


}


@keyframes fadeIn {


  from {


      opacity: 0;


  }


  to {


      opacity: 1;


  }


}


*, *:before, *:after {


  box-sizing: border-box;


  margin: 0;


  padding: 0;


}



html, body, main {


  height: 100%;


  width: 100%;


  color: white;


  font-size: 18px;


  font-weight: 400;


  font-family: 'Open Sans', 'Helvetica Neue', 'Helvetica', sans-serif;


  background-color: #008C9E;


}



header {


  position: absolute;


  top: 0;


  left: 0;


  width: 60%;


  text-align: center;


  opacity: 0;


  -webkit-animation: fadeIn ease-in 1;


                  animation: fadeIn ease-in 1;


  -webkit-animation-fill-mode: forwards;


                  animation-fill-mode: forwards;


  -webkit-animation-duration: .5s;


                  animation-duration: .5s;


}


header .title {


  text-transform: uppercase;


  font-size: 1.2em;


  border: 5px solid white;


  padding: 1em;


  opacity: 0;


  -webkit-animation: fadeIn ease-in 1;


                  animation: fadeIn ease-in 1;


  -webkit-animation-fill-mode: forwards;


                  animation-fill-mode: forwards;


  -webkit-animation-duration: .5s;


                  animation-duration: .5s;


  -webkit-animation-delay: 1s;


                  animation-delay: 1s;


}



nav {


  position: absolute;


  top: 0;


  right: 0;


  width: 40%;


  overflow-y: scroll;


  background-color: #F6F6F6;


}


nav ul li {


  opacity: 0;


  -webkit-transition: .5s opacity;


  transition: .5s opacity;


}


nav ul li.visible {


  opacity: 1;


}


nav li a {


  font-weight: 400;


  display: block;


  text-decoration: none;


  padding: 2em;


  border-bottom: 1px solid #D8D8D8;


  color: #343838;


  -webkit-transition: 0.2s ease;


  transition: 0.2s ease;


}


nav li a span {


  font-weight: 300;


  display: block;


  font-size: .7em;


  color: #D8D8D8;


  font-style: italic;


}


nav li a:hover {


  background-color: #005F6B;


  color: white;


}


nav footer {


  color: #343838;


  text-align: center;


  padding: 1em;


  font-size: 0.7em;


}



@media all and (max-width: 791px) {


  header {


      position: inherit;


      width: 100%;


      top: none;


      left: none;


  }


  header .title {


      border: none;


      padding: 4em;


  }



  nav {


      position: inherit;


      width: 100%;


      top: none;


      right: none;


  }


}


</style>


   



      <main>


  <header class="billboard">


      <h1 class="title">Testing</h1>


  </header>


  <nav class="slide-nav">


      <ul>


          <li><a href="#">Home <span>Oh you like home?</span> </a></li>


          <li><a href="#">About <span>Lets talk about it.</span> </a></li>


          <li><a href="#">Clients <span>No one likes clients.</span> </a></li>


          <li><a href="#">Contact Us <span>But please don't.</span> </a></li>


          <li><a href="#">Portfolio <span>No one does that anymore?</span> </a></li>


          <li><a href="#">Haircuts <span>Do I need to get one?</span> </a></li>


          <li><a href="#">Cars <span>You can drive one.</span> </a></li>


          <li><a href="#">Owls <span>Are terrorifying.</span> </a></li>


      </ul>


          </nav>


        </main>


      <script></script>


   


      <footer>


                ${AMP}copy; 2014 mattladner.com


          </footer>



        "</j:jelly>"


---------------------------------------------------------



UI Script code:



//That junt needs to happen on load and window resize


$(window).on("resize", function () {


 


      //Measure some responsiveness


      var responsive_viewport = $(window).width();




      // if is larger than 791px


      if (responsive_viewport > 791) {


     


          //Let's make some stuff fullscreen


          $('.billboard, .slide-nav').css('height', window.innerHeight);


     


          //Let's center up that h1 and do some measuering


          jQuery.fn.center = function () {


              this.parent().css("position","absolute");


              var t = this.parent().css("top");


              var l = this.parent().css("left");




              this.css("position","absolute");


              this.css("top", ((this.parent().height() - this.outerHeight()) / 2) + this.parent().scrollTop() + "px");


              this.css("left", ((this.parent().width() - this.outerWidth()) / 2) + this.parent().scrollLeft() + "px");


              return this;


          }




          //Center that junt


          $("h1").center();


     


      }



}).resize(); //fire off that resize event cause desingers always be stretching windows and stuff




//Load those nav li's sequrntially for purdyness


window.onload = function() {


  var cars = document.querySelectorAll(".slide-nav ul li"), i = 1;


      Array.prototype.forEach.call(cars, function(car) {


      setTimeout(function(){ car.classList.add("visible") }, 100*i)


      i++;


  })


};




find_real_file.png



Will greatly appreciate your advice.