Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

20 October 2013

Preserving The dynamically Added Content When Revisited Using Back Button

The problem with back button is, When user clicks on the back button to revisit the page, the browser displays the cached page. When it displays the cached page all the content that we added dynamically to the html page using javascript on the client side will be lost. For example look at the following html.

HTML with dynamic content added on client side using javascript:-
<html>
   <head>
     <script>
       function insert(){
          var str="<div>Enter The Value :<input type='text' name='magic'></div>";
           document.getElementById("sky").innerHTML=str;
       }
     </script>
   </head>
   <body>
     <form action="xxx" method="post">
       Name : <input type="text"  name="name" >
       <div id="sky" onclick="insert()">Press here for magic</div>
       <input type="submit" value="submit" >
     </form>
   <body>
</html>

In the above html when the user clicks on the magic “div” the insert function gets fired and it inserts a div which contains some elements into the magic div. Now after this the user moved to the next page and he clicked the browsers back button to revisit this page. When he comes to this page again using browsers back button all the content that we added to this html using javascript will be lost as they are added at runtime. So to prevent this we can use few efficient techniques.

I mean if you want to restore the page when user revisited using browsers back button  with all the dynamic content then we can use Ajax or Cookies or Hidden fields to solve this problem. which technique to use will be based on your requirement. To restore the content fire ajax or javascript function on Page load. Even when the browser is loading the html from the cache the loading events gets fired so you can use any method to store the state and restore them using javascript or ajax by calling the functions when onload event gets fired.

Using Ajax to Deal this problem:-
Use the command pattern so that each method which modifies the page's UI by adding controls also invokes an AJAX method to push the method invoked (textual Javascript representation) onto a queue stored in the server's session.

After body onLoad completes, use an AJAX method to query the server's session for a command queue for the page the user is on. If one is retrieved, just eval each member of the queue to rebuild the page's UI in the same order the user did it.

For complete thread on how to solve using Ajax please follow the thread below:-
http://stackoverflow.com/questions/1724739/back-button-handle-a-dynamic-form/1724780#1724780



10 October 2013

Tips in designing cross browser html coding

Always add  <!DOCTYPE html> (this is the doc type for html-5) as the first statement in your html file so that browsers do not fall back to primary versions of html. Especially in IE if you dont add the doc type statement “div” alignments will not work as it evaluates and treats your html code as IE5 (quircks) mode which is a very old version.

 When defining “font-family” in your css don’t use generic family types like “serif”, “monospace”. This causes problems and gives different look and feel which is ugly. Especially in IE. Use only specific font families like “Times New Roman” as font families. 

 These two things did a great difference in my design especially when IE is considered.

01 October 2013

Creating an Arrow And Circle Just With Html And CSS

Just with CSS we can create arrow marks. The following is the html code.


Code:-

<div style="width:0px;height: 0px;border-left: 15px solid transparent;border-right: 15px solid transparent;display: inline-block;zoom: 1;margin-left: 10px;float: left;border-bottom: 15px solid #999999;"></div>

The following is how it appears in the browser:-







The following is the code for creating a circle:-
<div style="float:left;width:50px;height:50px;">
<a style="background: #8f58a4;border-radius: 50%;cursor: pointer;display: inline-block;height: 100%;margin: 0;width: 100%;"></a>
</div>
Appearance in browser:-

18 September 2013

Color Code For Transparency

Sometimes we need the background to be transparent and to do that we need color code for making it transparent.


The color code for transparency is  : #EDEDED


This might work only for images with extension “.png”.

26 May 2013

Bringing Html Elements Into Single Line

Sometimes HTML elements even though we have not applied breaks they move into separate lines so if we have to bring them into a single line the following is the code:-

The following brings 3 div's into a single line:-

<div>
<div style="display:inline-block"> happy home</div>
<div style="display:inline-block"> singapoore</div>
<div style="display:inline-block"> India </div>
</div>

Output of the above code:-

happy home 
singapoore 
India


24 May 2013

Placing DIV's side by side without effecting the container DIV's height

I find the following as the best solution for such requirement.


<div style="border:2px solid red; overflow: hidden;">
       <div style="width:100px;height:100px;border:2px solid yellow;float:left;"></div>
       <div style="width:100px;height:150px;border:2px solid black;float:left">   </div>
       <div style="width:100px;height:100px;border:2px solid black;float:left">   </div>
 </div>

Actually "float" property when applied to child it erases the parent default properties. When "overflow:hidden" is placed in the parent element the default behavior will be restored.