SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 40
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Beginning JS Basics questions

    Which is best practice or format? Does the script have to be all on one line or can it be on many separate lines like this?

    Code:
    document.write("Today's date is " 
    return+  (RightNow.getMonth()+1)+ 
    return"-" + RightNow.getDate() + "-" 
    return+ RightNow.getFullYear() + ".  
    returnYou entered this Web Page at exactly: " 
    return+ RightNow.getHours() +
    return":" + RightNow.getMinutes()  + " and " 
    return+ RightNow.getSeconds() +
    return" seconds")

  2. #2
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would say multiple lines are better as its more readable instead of all in single lines.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    Which is best practice or format? Does the script have to be all on one line or can it be on many separate lines like this?
    It can be on separate lines, but it looks like you're doing something wrong with those return parts.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, on another issue, concerning the document.write object and method, when using it, if you wish to use it to write "multiple" lines of text at different times in one script, do you use it only once or do have to use it each time you need to write the text?

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    Thanks, on another issue, concerning the document.write object and method, when using it, if you wish to use it to write "multiple" lines of text at different times in one script, do you use it only once or do have to use it each time you need to write the text?
    It's normally advised to stay away from document.write as there are almost always safer techniques to use.

    Having said that though, it's more efficient to add the content you want to output to a variable, and to then output that at the end of things.

    Code javascript:
    var html = '';
    ...
    html += '<p>Some text</p>';
    ...
    html += '<p>Some more text</p>';
    ...
    document.write(html);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am studying JS from an outdated source:

    JavaScript Primers #3

    but that is the only source which I am understanding so I have to deal with things a little differently when it comes to modern day JS just so you understand what I am dealing with, I haven't gotten to var yet, I am following the course as structured. So, would it be possible for you to break down any further what what you have written please?

    If so, my questions, using your example would be:

    1] To begin using the write method, should I use this var html = '';
    every time?

    2] What are the 3 dots for between each line?

    3] Should I always write this html += '<p>Some text</p>'; when referring to text?

    4] If you are saying I should stay away from the document.write then why use this document.write(html); at the end?

    late here now, will check back in the morning for any answers, thanks!

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    So, would it be possible for you to break down any further what what you have written please?
    Sure thing. I just had a look at your source, and it appears that the "return" parts from what you quoted, are actually the image that was used on the web page to indicate a new line.

    I won't get in to the pros/cons of document.write just yet. Instead, here's something about what you were asking, the formatting of a statement.

    The code from your sample page should instead be:

    Code javascript:
    write("Today's date is " +  
    (RightNow.getMonth()+1)+ 
    "-" + RightNow.getDate() + "-" 
    + RightNow.getFullYear() + ".
    You entered this Web Page at exactly: " 
    + RightNow.getHours() +
    ":" + RightNow.getMinutes()  + " and " 
    + RightNow.getSeconds() +
    " seconds");

    That could also be all on one line:

    Code javascript:
    document.write("Today's date is " + (RightNow.getMonth()+1)+ "-" + RightNow.getDate() + "-" + RightNow.getFullYear() + ". You entered this Web Page at exactly: " + RightNow.getHours() + ":" + RightNow.getMinutes()  + " and " + RightNow.getSeconds() + " seconds");

    Or it could be split up like this:

    Code javascript:
    document.write(
        "Today's date is " +
        (RightNow.getMonth() + 1) + "-" + 
        RightNow.getDate() + "-" +
        RightNow.getFullYear() +
        ". You entered this Web Page at exactly: " + 
        RightNow.getHours() + ":" + 
        RightNow.getMinutes()  + " and " + 
        RightNow.getSeconds() + " seconds"
    );

    Notice how it's easier to understand that last example? The main purpose of formatting is to reduce confusion when you and other people read your code. One of the best ways to reduce confusion, is to follow a style guide or code conventions for your code. These help to enforce good practices, so that confusion is reduced to a minimum.


    A better online learning resource that you may want to consider has been receiving high praise lately. You may want to check out Eloquent JavaScript instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    I just had a look at your source, and it appears that the "return" parts from what you quoted, are actually the image that was used on the web page to indicate a new line.
    Sorry, I don't understand, any other wya you can explain the purpose of "return"?

    Also, I looked at your link but that seems way too technical for me so I will stick with the one I am using for now but thanks anyway.

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    Sorry, I don't understand, any other wya you can explain the purpose of "return"?
    Sure. Let's go back to the code that you posted, which started all of this.

    Code:
    document.write("Today's date is " 
    return+  (RightNow.getMonth()+1)+ 
    return"-" + RightNow.getDate() + "-" 
    return+ RightNow.getFullYear() + ".  
    returnYou entered this Web Page at exactly: " 
    return+ RightNow.getHours() +
    return":" + RightNow.getMinutes()  + " and " 
    return+ RightNow.getSeconds() +
    return" seconds")
    Whereever you see the word "return" in there, they should not be there. Those are the images on the web page that look like this:

    Those don't belong in your code, so when you copy the code from their web page, you'll need to watch out for those images and get rid of the extra text that they naughtily add to the code.

    Note though that there is also a keyword called return, that's used to return a value from a function. See Javascript Basics Part 3 for an example of that, in the validateForm function.

    So in all, watch out for on the web page, and be sure to remove what they add.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very well explained, thanks! On to Primer 4

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On Primer 11 now:

    JavaScript Primers #11


    Code:
    <SCRIPT type="text/javascript" >
    
    window.open('opened.html', 'joe','height=300,width=300,resizable=no')
    
     </SCRIPT>
    I can still resize the windows, any reason why the resizabe attribute is not working please?

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    I can still resize the windows, any reason why the resizabe attribute is not working please?
    Please read the documentation concerning the windows.open resizable setting.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  13. #13
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks but what I should have asked is "why" would someone NOT want the window to be resizeable?

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    Ok, thanks but what I should have asked is "why" would someone NOT want the window to be resizeable?
    The link from the documentation to the bug report seems to make a similar case.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Last issue on this topic please, I am having trouble understanding these two since I cannot see it happening I think:

    6. location=yes or no
    Whether or not you wish to show the location box with the current url (The place to type http://address).

    7. directories=yes or no
    Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).


    Can you give me an example of both? When I use those 2 attributes I don't see anything on my end?

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    6. location=yes or no
    Whether or not you wish to show the location box with the current url (The place to type http://address).
    If you look on the same page of the window.open documention, you will see that in section #7 it says that Firefox now always forces location to be shown.

    Quote Originally Posted by mikehende View Post
    7. directories=yes or no
    Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).
    The same section of the documentation page says that directories is now obselete.
    It was supported in old web browsers up to IE6, but not in more recent ones.

    There's little that I can add to the information that you get from there.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #17
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not really what I am asking, let me try another way, what "location box" is this referring to, the new pop up window? If yes then what "current URL" is this referring to? Does this all mean whether or not the pop up window should show the URL of the page you are reading from?

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    Not really what I am asking, let me try another way, what "location box" is this referring to, the new pop up window?
    Yes

    Quote Originally Posted by mikehende View Post
    If yes then what "current URL" is this referring to?
    The one of the popup window.

    Quote Originally Posted by mikehende View Post
    Does this all mean whether or not the pop up window should show the URL of the page you are reading from?
    That's right.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  19. #19
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool! Regarding this:

    7. directories=yes or no
    Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).

    When I use the directories=yes I am not seeing the "extra buttons" mentioned, can you elaborate on this please?

  20. #20
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    Cool! Regarding this:

    7. directories=yes or no
    Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).

    When I use the directories=yes I am not seeing the "extra buttons" mentioned, can you elaborate on this please?
    I've tried to direct you to the information twice already on the documentation page. Now it's time to blatently dump that info here.



    window.open - Toolbar and chrome features

    directories
    Obsolete in Gecko 2
    Obsolete synonym of personalbar. In IE, it rendered the Links bar. Supported in Gecko up to 1.9.2 and in IE up to 6.
    I elaborated earlier in saying that it only works in old web browsers like IE6. Anything newer than that and it doesn't work.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  21. #21
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I got that from before from the link but I am still trying to see or have an idea of what it looked like just for the knowledge, just because something is obsolete doesn't mean I don't wish to know about it. Even when I started studying the A+ exams for my pc repair career back in the 90s and they referred to computer stuff back in the 80s, I still wanted to know what they were talking about, just out of curiosity or to have the knowledge.

    Alright, I'll keep searching the net to see if I can get a screenshot of what this is about, thanks for help as always, be in touch with other issues as I go along!

  22. #22
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by mikehende View Post
    I am still trying to see or have an idea of what it looked like just for the knowledge
    The links bar you might also know of as the bookmarks toolbar, or favorites bar.
    See: Internet Explorer- Links bar
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  23. #23
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PERFECT! understood, thanks!

  24. #24
    SitePoint Zealot
    Join Date
    Feb 2011
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is an entire script, can anyone tell me exactly why it is not opening the new window please?
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
    <head>
    <meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
    <title>FunKaDeLic</title>
    <link type="text/css" rel="stylesheet" href="funkadelic.css" />
    
    <SCRIPT type="text/javascript" >
    
    function openindex ()
    {
    
    var name=prompt("What is your name?","Write it here")
    
    var OpenWindow=window.open("", "newwin", 
    "height=300,width=300");
    
    OpenWindow.document.write("<HTML>")
          OpenWindow.document.write("<TITLE>")
          OpenWindow.document.write
    	   ("Hello " +name+ " Here is your window!")
          OpenWindow.document.write("</TITLE>")
          OpenWindow.document.write("<BODY BGCOLOR=green>")
    
    OpenWindow.document.write("<CENTER>")
          OpenWindow.document.write
    	   ("<font size=+1>New Window</font><P>")
          OpenWindow.document.write
    	   ("<a href='' onClick='self.close()
    	   '>This closes the window</a><p>")
          OpenWindow.document.write("</CENTER>")
          OpenWindow.document.write("</BODY>")
          OpenWindow.document.write("</HTML>")
    
    }
     </SCRIPT>
    
    
    </head>
    
    <body>   
    
    onLoad="openindex ()"
    
    </body>
    
    </html>

  25. #25
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think one problem was you didn't have the ending semi-colons on each line...

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    >
    <
    html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
        <
    head>
            <
    meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
            <
    title>FunKaDeLic</title>
            <
    link type="text/css" rel="stylesheet" href="funkadelic.css" />
            <
    script type="text/javascript" >
                function 
    openindex() {
                    var 
    name prompt("What is your name?","Write it here");

                    var 
    OpenWindow=window.open("""newwin""height=300,width=300");
                        
    OpenWindow.document.write("<HTML>");
                        
    OpenWindow.document.write("<TITLE>");
                        
    OpenWindow.document.write("Hello " +name" Here is your window!");
                        
    OpenWindow.document.write("</TITLE>");
                        
    OpenWindow.document.write("<BODY BGCOLOR=green>");

                        
    OpenWindow.document.write("<CENTER>");
                        
    OpenWindow.document.write("<font size=+1>New Window</font><P>");
                        
    OpenWindow.document.write("<a href='' onClick='self.close()'>This closes the window</a><p>");
                        
    OpenWindow.document.write("</CENTER>");
                        
    OpenWindow.document.write("</BODY>");
                        
    OpenWindow.document.write("</HTML>");
                }
            
    </script>
        </head>
        <body>

        <script type="text/javascript">
            window.onload = function() {
                openindex();
            };
        </script>
        </body>
    </html> 


Bookmarks

Posting Permissions

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