Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: title, please!

  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Seoul, Korea
    Posts
    329
    Thanks
    0
    Thanked 0 Times in 0 Posts

    title, please!

    Code:
      <script language = "JavaScript" type = "text/javascript">
    
    	  function validate(f) {
    	  if (f.message.value.length == 0) {
    	  alert("Message, please");
    	  return false;
    	  }
    	  return true;
    	  }
    
        </script>
    
    
        <form action="action.cfm" method="post" onsubmit="return validate(this);">
    
    	 <input name="title" size="70"><br> 
    
    	  <textarea name="message"
            	    cols="70"
                    rows="10"></textarea><br>
    			  
          <input type="submit" value="Submit"> 
    
        </form>
    If a user clicks the submit button without entering any message,
    an alert saying "message, please" will come with the code above.


    I like to make an alert for the title, for example, "title, please" if the title box is empty.

    How can I make the alert not only for message but also for title?
    Get my greedy up

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function validate(f) {

    if (f['title'].value.length == 0) {
    alert("Title, please");
    return false;
    }
    if (f['message'].value.length == 0) {
    alert("Message, please");
    return false;
    }



    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You missed a bracket:

    function validate(f) {
    if (f['title'].value.length == 0) {
    alert("Title, please");
    return false;
    }
    if (f['message'].value.length == 0) {
    alert("Message, please");
    return false;
    }
    return true;
    }


    And... Frankly, I would have used still the full reference to avoid possible missunderstandings
    i.e.: f.elements['title'].value.length
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Seoul, Korea
    Posts
    329
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    f.elements['title'].value.length

    would you rephrase the quote in plain English?
    Get my greedy up

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    Cheshire, England
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    He's basically saying (to my understanding) that instead of using

    Code:
    f.['title'].value.length
    You use

    Code:
    f.elements['title'].value.length
    I guess anyway. Maybe it's a more secure method.
    m crilly
    http//www.cheesynimrod.com/

    "There is no such thing as a problem, only an opportunity for a solution."

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are also looking for trouble by naming your input title.....

    There is already a top level element by that name... document.title....

    .....Willy

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by Willy Duitt
    You are also looking for trouble by naming your input title.....

    There is already a top level element by that name... document.title....

    .....Willy
    There will be no conflict if you use title as the input name.
    The title property of document belongs to the document object while the other belongs to the elements collection. They are in a different level of document hierarchy.

    document title -> document['title']
    input named title -> document.forms[0].elements['title'] or document.forms['title']

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    document.forms[0].elements['title'] or document.forms['title']
    sorry to correct, you but they're not the same thing. the latter refers to a form named "title".

    wot you really meant was document.forms[0]['title'] instead
    *keep it simple (TM)

  • #9
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aye, I understand the theory but I still do not think it is a good idea.... Particularly since I see many people simply reference element names without declaring the collection they may belong too... Whether that collection be forms, images or links.... I do not have the script handy, but I do recall running into trouble using title as a name.... But I may have used it for a variable name.... I forget, but I have had problems with this in the past....
    .....Willy

  • #10
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Willy Duitt
    Aye, I understand the theory but I still do not think it is a good idea
    it's definitely a best practice to avoid using names which are also used by other objects or properties. even when code is syntactically correct it isn't necessarily well-written or intelligble. anything leads to confusion in property assignment can't have the best naming convention to begin with.

    hence it's best not to use "title" as an object name.
    *keep it simple (TM)

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I always thought that is a good practice to avoid reserved words for functions and even for names or ids...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Remember that IE pollutes the global namespace with every ID and every NAME so a form field named "title" is indeed going to conflict with (and override) the use of document.title for all users of that particular browser. While it should be in a different level of the browser heirarchy and thus quite ok for such use, IE will definitely mess that up.

    I bring this up because we had a company wide template sent out for use on all web pages here but someone decided that label an image (used as a link, don't get me started on that one) with a <img name="forms" ...> which means that document.forms now points to that image instead of to the forms collection making the scripting of any forms on pages using that template (which is required, did I mention that?) much more difficult.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by Roy Sinclair
    Remember that IE pollutes the global namespace with every ID and every NAME so a form field named "title" is indeed going to conflict with (and override) the use of document.title for all users of that particular browser. While it should be in a different level of the browser heirarchy and thus quite ok for such use, IE will definitely mess that up.
    I fully agree that you should try to avoid using reserved words as variable names even though it would not conflict to avoid confusion.

    My only point here is that using title as variable name will not conflict with document.title in IE. Although you can create a variable using document.myvariable (this technique is used in dreamweaver), you can only access it as that and not directly as myvariable except when it is called directly within an element. And I found in my test that ids or names of HTML controls such as textbox, textarea, ...etc are not treated as global variables, only ids of div, span and other non-control tags. Try testing this demo page in IE to see the result.
    Code:
    <html>
    <head>
    <title>This is the document title</title>
    <script type="text/javascript">
    document.myvariable="This is the content of document.myvariable";
    function displayTitle(){
      try{
        alert(title.value);
      }
      catch(e){
        alert("ERROR!\n"+e.message);
      }
    }
    function displayMyVar(){
      try{
        alert(myvariable);
      }
      catch(e){
        alert("ERROR!\n"+e.message);
      }
    }
    </script>
    </head>
    <body>
    <form>
    <div id="mydiv" style="border:1px solid red">This is a div with id of 'mydiv'.</div>
    <input type="text" name="title" id="title" value="This is a field with name and id of 'title'" size="50" /><br />
    <input type="button" value="document.title" onclick="alert(document.title)" /><br />
    <input type="button" value="document.myvariable" onclick="alert(document.myvariable)" /><br />
    <input type="button" value="myvariable" onclick="displayMyVar();alert('2nd function call (myvariable directly accessed by button):\n'+myvariable)" /><br />
    <input type="button" value="title.value" onclick="displayTitle()" /><br />
    <input type="button" value="mydiv.innerHTML" onclick="alert(mydiv.innerHTML)" />
    </form>
    </body>
    </html>

  • #14
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Still I think it is wiser to avoid reserved words. Or use upper cased charcters and/or numbers

    oTitle
    sSelect
    myForm
    divX
    id01
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #15
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FWIW: I remembered the script where I ran into trouble with using title....

    In the below example I passed the image.title as an arguement (title) to the function and the document.title would show when this was used as the title of the new window... The solution was to change the arguement name from title to something else....

    Code:
    <BASE HREF="http://www.xs4all.be/~chadier/webgallery_1/">
    <script type="text/javascript">
     <!--//
      function display(image,title){
       var theImage = new Image();
           theImage.src = image.replace(/thumbnails/i,'images');
           theImage.onerror = function(){
              alert('This image has failed to load!');
           }
    
           theImage.onload = function(){
            var width  = theImage.width;  if(width <100) { width  = 100 };
            var height = theImage.height; if(height<100) { height = 100 };
            var left = Math.round((screen.width - width) / 2);
            var top  = Math.round((screen.height - height) / 2);
            var features = 'top='+top+',left='+left+',width='+width+',height='+height;
            var openWindow = window.open('','display',features);
             with(openWindow.document){
               writeln('<html><head><title>'+title+'</title></head>');
               writeln('<body onload="self.focus()" onblur="self.close()"');
               writeln('style="background-image:url('+theImage.src+');');
               writeln('background-repeat:no-repeat;width:'+width+';height:'+height+'">');
               writeln('<div style="position:absolute;bottom:3px;text-align:center">');
               writeln('<span style="font-size:12px;width:100%">');
               writeln('<a href="javascript:self.close()">Close</a>');
               writeln('</span></div></body></html>');
               close();
             }
           }
               return false;
      }
     //-->
    </script>
    </head>
    
    <body>
    <img src="thumbnails/17.jpg" width="55" height="70"
    onclick="return display(this.src,this.title)"
    style="cursor:pointer;cursor:hand;border:0"
    title="Small Wrought Iron Table"
    alt="Thumbnail Image">  
    <img src="thumbnails/18.jpg" width="55" height="70"
    style="cursor:pointer;cursor:hand;border:0"
    onclick="return display(this.src,this.title)"
    title="Large Wrought Iron Table"
    alt="Thumbnail Image"> 
    <img src="thumbnails/19.jpg" width="55" height="70"
    style="cursor:pointer;cursor:hand;border:0"
    onclick="return display(this.src,this.title)"
    title="Wrought Iron Sculpture"
    alt="Thumbnail Image">
    ......Willy


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •