code embedded in html file vs .js file reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comcast news

    code embedded in html file vs .js file reference

    at least i think it this is the problem
    am trying to fix a webpage/javascript that i didn't write
    www.deltaneutral.com when invoked or any new page from the menu, get the
    error msg
    line 2
    char 1
    syntax error
    code 0
    there is a java scipt file invoked early on that 'seems' to be the problem,
    its just after the body tag
    <script language=javasc ript src=/scripts/swap.js></script>
    the code is at the end of this post
    however, when i embed the code directly in the page, instead of the file
    reference, the error goes away
    and the functions seem to execute ok
    i have checked the .js file with a hex editor looking for hidden weird bytes
    but havent' found any
    that look out of the ordinary, so i'm stumped why the seemingly same code
    gets the error msg
    only when it is invoked via its .js file
    also i have loaded the html and .js file on a different, 2nd web server just
    to be sure that somehow
    the primary server isn't somehow supplying the .js file with a funny byte



    <script type="text/javascript">
    //alert ("now entering swap2ansi");
    function WM_imageSwap(da Image, offOn){
    var daPath = "images/";
    var daPage = "home";
    // Check to make sure that images are supported in the DOM.
    if(document.ima ges){
    // Check to see whether you are using a name, number, or object
    if (typeof(daImage ) == 'string') {
    // This whole objStr nonesense is here solely to gain compatability
    // with ie3 for the mac.

    objStr = 'document.' + daImage;
    obj = eval(objStr);
    srcStr = daPath + "nav_" + daImage + "_" + offOn + ".gif";
    //srcEval = eval(daPath);
    obj.src = srcStr;

    objStr2 = 'document.' + daPage;
    obj2 = eval(objStr2);

    if (offOn == "on") {
    srcStr2 = daPath + "nav_" + daPage + "_off.gif";
    obj2.src = srcStr2;
    } else {
    srcStr2 = daPath + "nav_" + daPage + "_on.gif";
    obj2.src = srcStr2;
    }

    } else if ((typeof(daImag e) == 'object') && daImage && daImage.src) {

    daImage.src = daPath + "nav_" + daImage + "_" + offOn + ".gif";
    if (offOn == "on") {
    daPage.src = daPath + "nav_" + daPage + "_off.gif";
    } else {
    daPage.src = daPath + "nav_" + daPage + "_on.gif";
    }
    }
    }
    }


    function SimpleSwap(daIm age, daSrc){
    var objStr,obj;
    //alert ("now entering simple swap");
    // Check to make sure that images are supported in the DOM.
    if(document.ima ges){
    // Check to see whether you are using a name, number, or object
    if (typeof(daImage ) == 'string') {
    // This whole objStr nonesense is here solely to gain compatability
    // with ie3 for the mac.
    objStr = 'document.' + daImage;
    obj = eval(objStr);
    obj.src = daSrc;
    } else if ((typeof(daImag e) == 'object') && daImage && daImage.src) {
    daImage.src = daSrc;
    }
    }
    }
    //
    </script>



  • Michael Winter

    #2
    Re: code embedded in html file vs .js file reference

    On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <bxtrap01@comca st.net>
    wrote:
    [color=blue]
    > at least i think it this is the problem
    > am trying to fix a webpage/javascript that i didn't write
    > www.deltaneutral.com when invoked or any new page from the menu, get
    > the
    > error msg
    > line 2
    > char 1
    > syntax error
    > code 0
    > there is a java scipt file invoked early on that 'seems' to be the
    > problem,
    > its just after the body tag
    > <script language=javasc ript src=/scripts/swap.js></script>
    > the code is at the end of this post
    > however, when i embed the code directly in the page, instead of the file
    > reference, the error goes away
    > and the functions seem to execute ok
    > i have checked the .js file with a hex editor looking for hidden weird
    > bytes
    > but havent' found any
    > that look out of the ordinary, so i'm stumped why the seemingly same
    > code
    > gets the error msg
    > only when it is invoked via its .js file
    > also i have loaded the html and .js file on a different, 2nd web server
    > just
    > to be sure that somehow
    > the primary server isn't somehow supplying the .js file with a funny byte
    >
    >
    >
    > <script type="text/javascript">
    > //alert ("now entering swap2ansi");
    > function WM_imageSwap(da Image, offOn){
    > var daPath = "images/";
    > var daPage = "home";
    > // Check to make sure that images are supported in the DOM.
    > if(document.ima ges){
    > // Check to see whether you are using a name, number, or object
    > if (typeof(daImage ) == 'string') {
    > // This whole objStr nonesense is here solely to gain
    > compatability
    > // with ie3 for the mac.
    >
    > objStr = 'document.' + daImage;
    > obj = eval(objStr);
    > srcStr = daPath + "nav_" + daImage + "_" + offOn + ".gif";
    > //srcEval = eval(daPath);
    > obj.src = srcStr;
    >
    > objStr2 = 'document.' + daPage;
    > obj2 = eval(objStr2);
    >
    > if (offOn == "on") {
    > srcStr2 = daPath + "nav_" + daPage + "_off.gif";
    > obj2.src = srcStr2;
    > } else {
    > srcStr2 = daPath + "nav_" + daPage + "_on.gif";
    > obj2.src = srcStr2;
    > }
    >
    > } else if ((typeof(daImag e) == 'object') && daImage && daImage.src)
    > {
    >
    > daImage.src = daPath + "nav_" + daImage + "_" + offOn + ".gif";
    > if (offOn == "on") {
    > daPage.src = daPath + "nav_" + daPage + "_off.gif";
    > } else {
    > daPage.src = daPath + "nav_" + daPage + "_on.gif";
    > }
    > }
    > }
    > }
    >
    >
    > function SimpleSwap(daIm age, daSrc){
    > var objStr,obj;
    > //alert ("now entering simple swap");
    > // Check to make sure that images are supported in the DOM.
    > if(document.ima ges){
    > // Check to see whether you are using a name, number, or object
    > if (typeof(daImage ) == 'string') {
    > // This whole objStr nonesense is here solely to gain
    > compatability
    > // with ie3 for the mac.
    > objStr = 'document.' + daImage;
    > obj = eval(objStr);
    > obj.src = daSrc;
    > } else if ((typeof(daImag e) == 'object') && daImage && daImage.src)
    > {
    > daImage.src = daSrc;
    > }
    > }
    > }
    > //
    > </script>
    >
    >
    >[/color]



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

    Comment

    • Michael Winter

      #3
      Re: code embedded in html file vs .js file reference

      On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <bxtrap01@comca st.net>
      wrote:

      [snip]
      [color=blue]
      > its just after the body tag
      > <script language=javasc ript src=/scripts/swap.js></script>[/color]

      If this is the exact tag pair, the problem is that the src attribute value
      is not quoted. Quotes *must* be used when the value includes characters
      that are not among this set:

      - Letters
      - Numbers
      - Underscores (_)
      - Periods (.)
      - Hyphens (-)
      - Colons (:)

      Furthermore, the type attribute is required for the script element, and
      the language attribute is deprecated. The above should read:

      <script type="text/javascript" src="/scripts/swap.js"></script>

      You could also place it in the HEAD element so that it is guaranteed to be
      available to the document.

      [snip]

      The script below contains unnecessary eval() function calls. You should
      modify the code with the alternatives that I show below (comments have
      been stripped for clarity).
      [color=blue]
      > <script type="text/javascript">
      > function WM_imageSwap(da Image, offOn){
      > var daPath = "images/";
      > var daPage = "home";
      > if(document.ima ges){
      > if (typeof(daImage ) == 'string') {
      >
      > objStr = 'document.' + daImage;
      > obj = eval(objStr);[/color]

      daImage is a string that contains the name or id of an IMG element,
      correct? If so, it should be accessed using the images collection:

      obj = document.images[ daImage ];

      For future reference, you could have avoided the eval() call using:

      obj = document[ daImage ];

      If daImage was 'logo', for example, it would be the same as writing:

      obj = document.logo;
      [color=blue]
      > srcStr = daPath + "nav_" + daImage + "_" + offOn + ".gif";
      > //srcEval = eval(daPath);[/color]

      Image.src expects a string, so that eval() should never have been
      necessary. The assignment might as well be direct, too: the value isn't
      used elsewhere, so the variable is unnecessary.

      Both of intermediate variables (objStr and srcStr) have been declared
      global unnecessarily. Use the var keyword to keep them local.
      [color=blue]
      > obj.src = srcStr;
      >
      > objStr2 = 'document.' + daPage;
      > obj2 = eval(objStr2);[/color]

      This should probably be:

      obj2 = document.images[ daPage ];

      and if not, it should certainly be:

      obj2 = document[ daPage ];

      [snipped object branch]
      [color=blue]
      > function SimpleSwap(daIm age, daSrc){
      > var objStr,obj;
      > if(document.ima ges){
      > if (typeof(daImage ) == 'string') {
      > objStr = 'document.' + daImage;
      > obj = eval(objStr);[/color]

      I'm sure you can guess by now how this should be written.
      [color=blue]
      > obj.src = daSrc;
      > } else if ((typeof(daImag e) == 'object') && daImage && daImage.src)
      > {
      > daImage.src = daSrc;
      > }
      > }
      > }
      > //[/color]

      Informative comment! :P
      [color=blue]
      > </script>[/color]

      Hope that helps,
      Mike

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

      Comment

      • David Dorward

        #4
        Re: code embedded in html file vs .js file reference

        comcast news wrote:
        [color=blue]
        > at least i think it this is the problem
        > am trying to fix a webpage/javascript that i didn't write
        > www.deltaneutral.com when invoked or any new page from the menu, get
        > the error msg[/color]

        ..js files should contain JavaScript, not a combination of HTML and
        JavaScript.

        Get rid of the <script> tags from .js files.

        (and run the HTML through http://validator.w3.org/ )


        --
        David Dorward <http://dorward.me.uk/>

        Comment

        • Richard Cornford

          #5
          Re: code embedded in html file vs .js file reference

          Michael Winter wrote: <opr4hk9cxa5vkl cq@news-text.blueyonder .co.uk>
          <snip>[color=blue][color=green]
          >> <script type="text/javascript">
          >> function WM_imageSwap(da Image, offOn){[/color][/color]
          <snip>

          There are two other common causes of javascript moved to external files
          not working in that location when they work form a page. The first is
          including HTML in the external file, so if that opening script tag is in
          the external file it becomes a syntax error because it is not
          javascript. (SGML comment tags are also often syntax errors in external
          JS files)

          The other is badly configured servers, specifically older apache
          versions where files with JS extensions are not associated with a known
          content type so the server sends them as text/html (which doesn't matter
          in itself) but also inserts an HTML 2.0 doctype, which is again a syntax
          error in a javascript file. You don't see this second problem much these
          days.

          In both cases testing with Mozilla/Gecko browsers usually reveals the
          problem as its error messages include the offending line of code, so it
          is obvious when it is not javascript (or was not part of the original
          file).

          Richard.


          Comment

          Working...