Putting "javascript:" in front of code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • delerious@no.spam.com

    Putting "javascript:" in front of code?

    I see some code examples like this:

    <DIV onmouseover="th is.style.backgr ound='blue'">

    and other code examples like this:

    <DIV onmouseover="ja vascript:this.s tyle.background ='blue'">

    Which way is more proper? Or are both ways perfectly fine? Are there any
    specifications that discuss when "javascript :" should be put in front of code?

  • Janwillem Borleffs

    #2
    Re: Putting &quot;javascrip t:&quot; in front of code?


    <delerious@no.s pam.com> schreef in bericht
    news:3fd198f8.1 0126601@news.md .comcast.gigane ws.com...[color=blue]
    > I see some code examples like this:
    >
    > <DIV onmouseover="th is.style.backgr ound='blue'">
    >
    > and other code examples like this:
    >
    > <DIV onmouseover="ja vascript:this.s tyle.background ='blue'">
    >
    > Which way is more proper? Or are both ways perfectly fine? Are there any
    > specifications that discuss when "javascript :" should be put in front of[/color]
    code?[color=blue]
    >[/color]

    The first one is the proper way, because the onmouseover attribute (and
    other event handlers) expects the value to contain JS code only.

    What you will also see very often, is the usage of the javascript: pseudo
    protocol in anchors:
    <a href="javascrip t:popup('somepa ge')">...</a>

    which should be replaced with:
    <a href="somepage" onclick="popup( href); return false">...</a>

    because the link will still be clickable when JS is disabled. When JS is
    enabled, the function will be called with the href value as an argument and
    the 'return false' at the end prevents that the default action will be
    taken.

    See also: http://jibbering.com/faq/#FAQ4_24


    JW



    Comment

    • Michael Winter

      #3
      Re: Putting &quot;javascrip t:&quot; in front of code?

      Janwillem Borleffs wrote on 06 Dec 2003:
      [color=blue]
      >
      > <delerious@no.s pam.com> schreef in bericht
      > news:3fd198f8.1 0126601@news.md .comcast.gigane ws.com...[color=green]
      >> I see some code examples like this:
      >>
      >> <DIV onmouseover="th is.style.backgr ound='blue'">
      >>
      >> and other code examples like this:
      >>
      >> <DIV onmouseover="ja vascript:this.s tyle.background ='blue'">
      >>
      >> Which way is more proper? Or are both ways perfectly fine?
      >> Are there any specifications that discuss when "javascript :"
      >> should be put in front of code?[/color][/color]

      The second way is incorrect. "JavaScript :" is a URI protocol
      specifier, so it is only valid in a src or href attribute. However
      (as J Borleffs stated), JavaScript URIs should not be used in case
      JavaScript has been disabled by the user: the URI will be malformed
      and useless to the user.
      [color=blue]
      > The first one is the proper way, because the onmouseover
      > attribute (and other event handlers) expects the value to
      > contain JS code only.[/color]

      WHAT?!?! That is so WRONG it's untrue! The intrinsic events can be
      used by ANY scripting language: that's why they are part of the HTML
      specification, not solely JavaScript.

      The language used in intrinsic events is specified either with a HTTP
      header or, more commonly, a META element placed in the document's
      HEAD:

      <META http-equiv="Content-Script-Type" type="script_MI ME_type">

      For JavaScript, the MIME type is text/javascript, so the above should
      read:

      <META http-equiv="Content-Script-Type" type="text/javascript">

      If you omit either the header or META element, but use intrinsic
      events, your HTML document is invalid. The only reason why it would
      work is because the browser makes assumptions about what the
      intrinsic events contain (they usually assume JavaScript). However,
      not all may assume JavaScript as there are no 'default' languages. In
      fact, the HTML specification states clearly that NO assumption needs
      to be made and any events can be ignored, if no default language has
      been specified by the author (using META or HTTP header).

      <snipped the rest of the reply, which is OK>

      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

      Comment

      • Janwillem Borleffs

        #4
        Re: Putting &quot;javascrip t:&quot; in front of code?


        "Michael Winter" <M.Winter@bluey onder.co.invali d> schreef in bericht
        news:Xns94497BC 66DB74MWinterBl ueyonder@193.38 .113.46...[color=blue]
        >
        > WHAT?!?! That is so WRONG it's untrue! The intrinsic events can be
        > used by ANY scripting language: that's why they are part of the HTML
        > specification, not solely JavaScript.
        >[/color]

        Yeah, you're right of course.


        JW



        Comment

        • Janwillem Borleffs

          #5
          Re: Putting &quot;javascrip t:&quot; in front of code?


          "Michael Winter" <M.Winter@bluey onder.co.invali d> schreef in bericht
          news:Xns94497BC 66DB74MWinterBl ueyonder@193.38 .113.46...[color=blue]
          >
          > WHAT?!?! That is so WRONG it's untrue! The intrinsic events can be
          > used by ANY scripting language: that's why they are part of the HTML
          > specification, not solely JavaScript.
          >
          > The language used in intrinsic events is specified either with a HTTP
          > header or, more commonly, a META element placed in the document's
          > HEAD:
          >
          > <META http-equiv="Content-Script-Type" type="script_MI ME_type">
          >[/color]

          Did some research on this header, and it seems that at least IE totally
          ignores it and always defaults to JS.

          Try:

          <html>
          <head>
          <title> New Document </title>
          <meta http-equiv="Content-Script-Type" content="text/vbscript" />
          </head>

          <body>
          <a href="#" onclick="MsgBox ('Hello')">Say. ..</a>
          </body>
          </html>

          Read:




          JW



          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Putting &quot;javascrip t:&quot; in front of code?

            delerious@no.sp am.com writes:
            [color=blue]
            > I see some code examples like this:
            >
            > <DIV onmouseover="th is.style.backgr ound='blue'">
            >
            > and other code examples like this:
            >
            > <DIV onmouseover="ja vascript:this.s tyle.background ='blue'">
            >
            > Which way is more proper?[/color]

            The former. Personally, I would write:
            <div onmouseover="th is.style.backgr oundColor='blue ';">
            but that's leaving correct and passing on to pedantic :)
            [color=blue]
            > Or are both ways perfectly fine?[/color]

            The "javascript :" is not necessary, doesn't mean what the author think
            it means, and at best does no damage.

            The author probably thinks that it specifies that what comes after is
            Javascript. It doesn't [1]. Instead it is read by the Javascript
            interpreter as a label with the name "javascript :". You can try this
            (in a non-IE browser):

            <div onclick="javasc ript:while(true ){break javascript;};al ert('exit');">
            XXX
            </div>

            The "break javascript" statement breaks the loop labeled "javascript "
            by the initial label. It doesn't work in IE [1].
            [color=blue]
            > Are there any specifications that discuss when "javascript :" should
            > be put in front of code?[/color]

            Yes. The short summary is: Almost never.

            The place where it belongs is in javascript:-URL's. Example:
            <a href="javascrip t:generatePage( )">generate new page</a>
            The meaning of that is, that the result of the Javascript expression
            after the "javascript :" becomes the new content of the page.
            Anotheer example:
            <a href="javascrip t:'<p>this page has no content</p>'">generate
            new page</a>
            That form of javascript:-URL is often misused where the onclick
            event handler is more appropriate (see the FAQ:
            <URL:http://jibbering.com/faq/#FAQ4_24>).
            These URLs are very rarely needed in actual pages.

            Javascript: URLs are also very useful as bookmarks, where they can be
            used to execute code in the context of the current page (I have
            several, e.g.,
            javascript:aler t(document.comp atMode)

            The other time when you might need it is if you write a page
            specifically to IE, and where you have set the default script language
            to, e.g., VBScript. Your page will only work in IE, so you might
            as well use other IE-specific features, like the "javascript :" intrinsic
            event language selection. Such a page doesn't belong on the internet,
            but on intranets where you know only IE will access it, it can make
            sense.


            Summary: You can appropriately use "javascript :"
            - in href/URL - when it generates the new page, or
            - as bookmarks/favorites (aka. bookmarklets/favlets)
            - in onclick etc - in IE-specific pages only.

            You can most likely make a living as a web designer without ever
            using "javascript :" again.
            /L

            [1] IE is special. It actually uses the "javascript :" to specify the
            language of the following code. That is only necessary if the language
            is different from the default script language of the page, which by
            default is JScript/Javascript for IE. You set the default script
            language for intrinsic events (e.g., onclick) using, e.g.,
            <meta http-equiv="Content-Script-Type" content="text/javascript">
            Not a bad idea in itself, and if other browsers supported more than
            one language, it would probably become a standard. Right now it
            isn't, and IE is the only browser with this behavior.

            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Richard Cornford

              #7
              Re: Putting &quot;javascrip t:&quot; in front of code?

              "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
              news:Xns94497BC 66DB74MWinterBl ueyonder@193.38 .113.46...
              <snip>[color=blue][color=green][color=darkred]
              >>><DIV onmouseover="th is.style.backgr ound='blue'">
              >>>
              >>>and other code examples like this:
              >>>
              >>><DIV onmouseover="ja vascript:this.s tyle.background ='blue'">
              >>>
              >>>Which way is more proper? Or are both ways perfectly fine?
              >>>Are there any specifications that discuss when "javascript :"
              >>>should be put in front of code?[/color][/color]
              >
              >The second way is incorrect.[/color]

              Incorrect might be a bit strong as it implies something contrary to some
              criteria of validity or syntactic correctness. Though that may just be a
              matter of interpretation (of English).
              [color=blue]
              >"JavaScript: " is a URI protocol specifier, so it
              >is only valid in a src or href attribute.[/color]

              In JavaScript source code (which is what the string provided as the
              value for an event handling attribute is (assuming JavaScript is the
              default language for such attributes) a character sequence that
              qualifies as an identifier followed by a colon is a label[1] (ECMA 262
              3rd Ed. Section 12.12). And the character sequence "javascript " fulfils
              the requirements to be considered an identifier so "javascrpt: " will be
              interpreted as a label by conforming interpreters.

              As a result its inclusion at the start of the code for an event
              handeling attribute string is completely valid, in the sense of being
              completely legal JavaScript source code, it is just utterly pointless.
              Unless, of course, there was a corresponding - break - or - continue -
              statement that employed that label. However, IE unfortunately does place
              a special interpretation on the character sequence "javascrpt: "
              appearing in that context and does not use it as a label within the
              resulting event handling function. So while prefixing the string
              provided for an event handling attribute with "javascript :" normally
              represents the valid (but pointless) introduction of a label into the
              function the fact that IE treats it differently means that if the code
              should have a label in that context "javascript :" would be an unwise
              label to use (along with, for example, "vbscript:" ).

              So the inclusion of "javascript :" as a prefix for event handling
              attributes is incorrect (when used in a non-IE specific (Intranet)
              context) in the sense that including a label that is never used in
              source code is pointless and if a label was required "javascrpt: " would
              be predictably unsuitable as a label because of the way that it is
              handled by IE. Just 11 extra bytes of download which contribute nothing
              but to potentially induce confusion in the minds of inexperienced
              readers.
              [color=blue]
              >However (as J Borleffs stated), JavaScript URIs should not be
              >used in case JavaScript has been disabled by the user: the URI
              >will be malformed and useless to the user.[/color]
              <snip>

              JavaScript URIs are known causers of problems even when JavaScript is
              enabled/available (just another reason for never using them).

              Richard.

              [1] Except possibly when it occurs within a switch statement or the
              conditional ( ? : ) operator.


              Comment

              • Michael Winter

                #8
                Re: Putting &quot;javascrip t:&quot; in front of code?

                Richard Cornford wrote on 06 Dec 2003:
                [color=blue]
                > "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in
                > message news:Xns94497BC 66DB74MWinterBl ueyonder@193.38 .113.46...[/color]

                <snip>
                [color=blue][color=green]
                >>The second way is incorrect.[/color]
                >
                > Incorrect might be a bit strong as it implies something contrary
                > to some criteria of validity or syntactic correctness. Though
                > that may just be a matter of interpretation (of English).[/color]

                Incorrect, meaning: the wrong way of specifying the language of an
                intrinsic event.
                [color=blue][color=green]
                >>"JavaScript :" is a URI protocol specifier, so it
                >>is only valid in a src or href attribute.[/color]
                >
                > In JavaScript source code (which is what the string provided as
                > the value for an event handling attribute is (assuming
                > JavaScript is the default language for such attributes) a
                > character sequence that qualifies as an identifier followed by a
                > colon is a label[1] (ECMA 262 3rd Ed. Section 12.12). And the
                > character sequence "javascript " fulfils the requirements to be
                > considered an identifier so "javascrpt: " will be interpreted as
                > a label by conforming interpreters.[/color]

                [moved][color=blue]
                > [1] Except possibly when it occurs within a switch statement or
                > the conditional ( ? : ) operator.[/color]

                That's true, but the topic isn't (quite) relating to JavaScript
                source code. This is regarding the use of the string, "JavaScript :",
                to actually perform some specific action. The OP assumed that using
                JavaScript: in an intrinsic event specified the language of that
                event handling code (which is incorrect). The only special nature of
                the string should be as (defined by Netscape - so possibly not a
                global standard) a protocol specifier in the context of a URI.

                <snip>
                [color=blue]
                > JavaScript URIs are known causers of problems even when
                > JavaScript is enabled/available (just another reason for never
                > using them).[/color]

                Agreed

                Mike

                --
                Michael Winter
                M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

                Comment

                • Jim Ley

                  #9
                  Re: Putting &quot;javascrip t:&quot; in front of code?

                  On Sat, 06 Dec 2003 12:10:04 GMT, Michael Winter
                  <M.Winter@bluey onder.co.invali d> wrote:
                  [color=blue]
                  >For JavaScript, the MIME type is text/javascript, so the above should
                  >read:[/color]

                  Could you cite the relevant RFC which indicates this please? for
                  JavaScript (capatilised like that) the relevant one would be
                  application/x-javascript surely?
                  [color=blue]
                  >If you omit either the header or META element, but use intrinsic
                  >events, your HTML document is invalid.[/color]

                  No, invalid has a technical meaning in HTML, and that does not make it
                  invalid, it may make it a non-conforming HTML 4 document, but then
                  that's only a tiny subset of HTML. ( RFC 2854 blesses all tag-soup to
                  be html)

                  Jim.
                  --
                  comp.lang.javas cript FAQ - http://jibbering.com/faq/

                  Comment

                  • Lee

                    #10
                    Re: Putting &quot;javascrip t:&quot; in front of code?

                    Michael Winter said:[color=blue]
                    >
                    >Richard Cornford wrote on 06 Dec 2003:
                    >[color=green]
                    >> "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in
                    >> message news:Xns94497BC 66DB74MWinterBl ueyonder@193.38 .113.46...[/color]
                    >
                    ><snip>
                    >[color=green][color=darkred]
                    >>>The second way is incorrect.[/color]
                    >>
                    >> Incorrect might be a bit strong as it implies something contrary
                    >> to some criteria of validity or syntactic correctness. Though
                    >> that may just be a matter of interpretation (of English).[/color]
                    >
                    >Incorrect, meaning: the wrong way of specifying the language of an
                    >intrinsic event.[/color]

                    The OP didn't ask how to specify the language, he asked
                    which syntax was more proper.
                    [color=blue]
                    >That's true, but the topic isn't (quite) relating to JavaScript
                    >source code. This is regarding the use of the string, "JavaScript :",
                    >to actually perform some specific action.[/color]

                    That's not how I interpret the topic. I suspect the OP
                    didn't know that other languages were even available.

                    Comment

                    • Dauber!

                      #11
                      Re: Putting &quot;javascrip t:&quot; in front of code?

                      Waaaaay back on 06-Dec-03 03:58:11, delerious said this about Putting "javascript :" in front of code?:[color=blue]
                      >I see some code examples like this:[/color]
                      [color=blue]
                      ><DIV onmouseover="th is.style.backgr ound='blue'">[/color]
                      [color=blue]
                      >and other code examples like this:[/color]
                      [color=blue]
                      ><DIV onmouseover="ja vascript:this.s tyle.background ='blue'">[/color]
                      [color=blue]
                      >Which way is more proper? Or are both ways perfectly fine? Are there any
                      >specificatio ns that discuss when "javascript :" should be put in front of
                      >code?[/color]

                      The "javascript :" prefix, seriously, is a gift from Satan. It's horrible.
                      Most browsers I've used don't know what the fleeick to do with that.

                      If it works without the "javascript :" prefix, I say don't use it. If it
                      works WITH it, find an alternate way.

                      --
                      dauber@banana-and-louie.org * dauber.50megs.c om
                      * ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

                      Comment

                      • delerious@no.spam.com

                        #12
                        Re: Putting &quot;javascrip t:&quot; in front of code?

                        On Sat, 06 Dec 2003 15:00:01 +0100, Lasse Reichstein Nielsen <lrn@hotpop.com >
                        wrote:[color=blue][color=green]
                        >> Which way is more proper?[/color]
                        >
                        >The former. Personally, I would write:
                        > <div onmouseover="th is.style.backgr oundColor='blue ';">
                        >but that's leaving correct and passing on to pedantic :)[/color]

                        Oh this was another thing I was wondering about. :) I tried to do
                        "this.style.bac kground-color='blue'" but got an error. So in order to get
                        those CSS properties to work in Javascript, I should remove the hyphens and
                        capitalize the first letter of the words after hyphens?

                        Thanks for the great, informative responses everyone!

                        Comment

                        • Lasse Reichstein Nielsen

                          #13
                          Re: Putting &quot;javascrip t:&quot; in front of code?

                          delerious@no.sp am.com writes:
                          [color=blue]
                          > Oh this was another thing I was wondering about. :) I tried to do
                          > "this.style.bac kground-color='blue'" but got an error. So in order to get
                          > those CSS properties to work in Javascript, I should remove the hyphens and
                          > capitalize the first letter of the words after hyphens?[/color]

                          Yes, that is the way CSS properties are mapped into Style Object
                          properties in the W3C DOM (and supported in most modern browsers).
                          <URL:http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSS2Properties>

                          /L
                          --
                          Lasse Reichstein Nielsen - lrn@hotpop.com
                          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                          'Faith without judgement merely degrades the spirit divine.'

                          Comment

                          • Michael Winter

                            #14
                            Re: Putting &quot;javascrip t:&quot; in front of code?

                            Jim Ley wrote on 06 Dec 2003:
                            [color=blue]
                            > On Sat, 06 Dec 2003 12:10:04 GMT, Michael Winter
                            > <M.Winter@bluey onder.co.invali d> wrote:
                            >[color=green]
                            >>For JavaScript, the MIME type is text/javascript, so the above
                            >>should read:[/color]
                            >
                            > Could you cite the relevant RFC which indicates this please?
                            > for JavaScript (capatilised like that) the relevant one would be
                            > application/x-javascript surely?[/color]

                            The HTML specification itself uses text/javascript as the MIME type
                            for JavaScript (though that doesn't necessarily make it correct).

                            This issue was raised in W3C's public HTML mailing list two years ago
                            (http://lists.w3.org/Archives/Public/...Sep/0020.html). It
                            comments that then, various script MIME types (of both text and
                            application types) were not registered. Looking on IANA's website, I
                            found that they're /still/ not registered. Mentioned in that mailing
                            list was the fact that browsers are split on what types to use: IE
                            with text and Netscape with application. However, as neither types
                            have been registered, neither browser is correct. I don't know what
                            various browsers consider the proper JavaScript MIME type. However, I
                            do agree that an application type is the better choice.
                            [color=blue][color=green]
                            >>If you omit either the header or META element, but use intrinsic
                            >>events, your HTML document is invalid.[/color]
                            >
                            > No, invalid has a technical meaning in HTML, and that does not
                            > make it invalid, it may make it a non-conforming HTML 4
                            > document, but then that's only a tiny subset of HTML. ( RFC
                            > 2854 blesses all tag-soup to be html)[/color]

                            Do the semantics really matter? Yes, if you attempted to validate the
                            document it would succeed, but the document is still incorrect and
                            relies on behaviour that is not guaranteed.

                            Mike

                            --
                            Michael Winter
                            M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

                            Comment

                            • Michael Winter

                              #15
                              Re: Putting &quot;javascrip t:&quot; in front of code?

                              Lee wrote on 06 Dec 2003:
                              [color=blue]
                              > Michael Winter said:[color=green]
                              >>
                              >>Richard Cornford wrote on 06 Dec 2003:
                              >>[color=darkred]
                              >>> "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in
                              >>> message
                              >>> news:Xns94497BC 66DB74MWinterBl ueyonder@193.38 .113.46...[/color]
                              >>
                              >><snip>
                              >>[color=darkred]
                              >>>>The second way is incorrect.
                              >>>
                              >>> Incorrect might be a bit strong as it implies something
                              >>> contrary to some criteria of validity or syntactic
                              >>> correctness. Though that may just be a matter of
                              >>> interpretation (of English).[/color]
                              >>
                              >>Incorrect, meaning: the wrong way of specifying the language of
                              >>an intrinsic event.[/color]
                              >
                              > The OP didn't ask how to specify the language, he asked
                              > which syntax was more proper.[/color]

                              I know what the OP asked.
                              [color=blue][color=green]
                              >>That's true, but the topic isn't (quite) relating to JavaScript
                              >>source code. This is regarding the use of the string,
                              >>"JavaScript:" , to actually perform some specific action.[/color]
                              >
                              > That's not how I interpret the topic. I suspect the OP
                              > didn't know that other languages were even available.[/color]

                              The original question was: Is "javascript:som e_javascript_co de"
                              syntactically correct? The answer is yes. But leaving it at that
                              neglects the assumption upon which the inclusion of "javascript :" is
                              based. Some authors that post to this group attach a special meaning
                              to that string in an intrinsic event, and that association needs to
                              be dispelled.

                              Mike

                              --
                              Michael Winter
                              M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

                              Comment

                              Working...