Need to keep the form always in focus when clicking the link for it ?

Discussion in 'JavaScript' started by Kezi, Mar 25, 2009.

  1. #1
    Hi all, Wondered if any of you kind souls could give any quick help please ? The code below, is part of a php include which appears on every page on the site. The trouble is, is that some pages are very long and some are very short, so using offsetTop is obviously not bringing the form into full view ;-( Still keeping all the functions, how would you adjust the code to achieve this aim please ?
    <script type="text/javascript">
    function createCookie(name,value,days) {
    	if (days) {
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    function addOnloadEvent(fnc){
      if ( typeof window.addEventListener != "undefined" )
        window.addEventListener( "load", fnc, false );
      else if ( typeof window.attachEvent != "undefined" ) {
        window.attachEvent( "onload", fnc );
      }
      else {
        if ( window.onload != null ) {
          var oldOnload = window.onload;
          window.onload = function ( e ) {
            oldOnload( e );
            window[fnc]();
          };
        }
        else
          window.onload = fnc;
      }
    }
    <?php
        if(isset($_GET['error']) || isset($_SESSION['dodisplay']))
        {
        ?>
    addOnloadEvent(function(){document.location.hash = "form";});
        <?php
        }
        ?>
        </script>
    <a href="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>?display=1#1form" onclick="document.getElementById('form').style.display = 'block';createCookie('display','block',0);window.scrollTo(0,document.getElementById('tellfriends').offsetTop - 120);return false">Click here for form</a>
    <form action="<?php echo (isset($_GET['display'])) ? '/form-page/index.php?display=1' : '/form-page/index.php'; ?>" method="post" name="form" id="form" style="display:<?php echo (isset($_GET['display']) || isset($_COOKIE['display']) || isset($_SESSION['dodisplay'])) ? 'block' : 'none'; ?>">
    <fieldset>
    <?php
    	if(isset($_GET['error'])) echo '<p class="errormsg">'.$_SESSION['errormsg'].'</p>';
    ?>
    Form fields etc, etc . . . . . . .
    Code (markup):
    Been pulling my hair out on this and googled everywhere on it, but still stumped, so any help would be really appreciated.

    Kezi
     
    Kezi, Mar 25, 2009 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi Kezi,

    this can be done without javascript, if you don't want to lose your hair.
    Simply <a href="#my_form" onclick="document.getElementById('form').style.display = 'block';createCookie('display','block',0);">Click here for form</a> creates link to named anchor

    And at the place where you have your form add the named anchor <a name="my_form"></a>

    Notice the mask '#' and the names in href and name attribute must be identical.
     
    lp1051, Mar 25, 2009 IP
  3. Kezi

    Kezi Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Many thanks for that, the helps appreciated.

    Tried :

    
    <a name="my_form"></a>
    <a href="#my_form" onclick="document.location.hash('#my_form').style.display = 'block';createCookie('display','block',0);">Click here for form</a>
    Code (markup):
    But it didn't work ?

    also tried :

    <a name="my_form"></a>
    <a href="#my_form" onclick="document.getElementById('form').style.display = 'block';createCookie('display','block',0);">Click here for form</a>
    Code (markup):
    and that didn't work either ? ?

    Kezi
     
    Kezi, Mar 26, 2009 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Okay... it seems this has been way too complicated.

    In the link, where you want the "Click here" text, do as follows:

    <a href="#form_name">Click here to go to form</a>

    And, at the form:

    <form id="form_name">

    It will go directly to the form when clicked
    No Javascript whatsoever, just normal HTML
     
    PoPSiCLe, Mar 26, 2009 IP