code embedded in html file vs .js file reference

Discussion in 'Javascript' started by comcast news, Mar 7, 2004.

  1. comcast news

    comcast news Guest

    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=javascript 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(daImage, offOn){
    var daPath = "images/";
    var daPage = "home";
    // Check to make sure that images are supported in the DOM.
    if(document.images){
    // 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(daImage) == '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(daImage, daSrc){
    var objStr,obj;
    //alert ("now entering simple swap");
    // Check to make sure that images are supported in the DOM.
    if(document.images){
    // 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(daImage) == 'object') && daImage && daImage.src) {
    daImage.src = daSrc;
    }
    }
    }
    //
    </script>
     
    comcast news, Mar 7, 2004
    #1
    1. Advertising

  2. On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <>
    wrote:

    > 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=javascript 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(daImage, offOn){
    > var daPath = "images/";
    > var daPage = "home";
    > // Check to make sure that images are supported in the DOM.
    > if(document.images){
    > // 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(daImage) == '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(daImage, daSrc){
    > var objStr,obj;
    > //alert ("now entering simple swap");
    > // Check to make sure that images are supported in the DOM.
    > if(document.images){
    > // 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(daImage) == 'object') && daImage && daImage.src)
    > {
    > daImage.src = daSrc;
    > }
    > }
    > }
    > //
    > </script>
    >
    >
    >




    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Mar 7, 2004
    #2
    1. Advertising

  3. On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <>
    wrote:

    [snip]

    > its just after the body tag
    > <script language=javascript src=/scripts/swap.js></script>


    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).

    > <script type="text/javascript">
    > function WM_imageSwap(daImage, offOn){
    > var daPath = "images/";
    > var daPage = "home";
    > if(document.images){
    > if (typeof(daImage) == 'string') {
    >
    > objStr = 'document.' + daImage;
    > obj = eval(objStr);


    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;

    > srcStr = daPath + "nav_" + daImage + "_" + offOn + ".gif";
    > //srcEval = eval(daPath);


    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.

    > obj.src = srcStr;
    >
    > objStr2 = 'document.' + daPage;
    > obj2 = eval(objStr2);


    This should probably be:

    obj2 = document.images[ daPage ];

    and if not, it should certainly be:

    obj2 = document[ daPage ];

    [snipped object branch]

    > function SimpleSwap(daImage, daSrc){
    > var objStr,obj;
    > if(document.images){
    > if (typeof(daImage) == 'string') {
    > objStr = 'document.' + daImage;
    > obj = eval(objStr);


    I'm sure you can guess by now how this should be written.

    > obj.src = daSrc;
    > } else if ((typeof(daImage) == 'object') && daImage && daImage.src)
    > {
    > daImage.src = daSrc;
    > }
    > }
    > }
    > //


    Informative comment! :p

    > </script>


    Hope that helps,
    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Mar 7, 2004
    #3
  4. comcast news wrote:

    > 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


    ..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/>
     
    David Dorward, Mar 7, 2004
    #4
  5. Michael Winter wrote: <>
    <snip>
    >> <script type="text/javascript">
    >> function WM_imageSwap(daImage, offOn){

    <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.
     
    Richard Cornford, Mar 7, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Paul Gloster
    Replies:
    48
    Views:
    1,971
    Colin Paul Gloster
    Apr 10, 2007
  2. =?Utf-8?B?UGF1bA==?=

    HTML Code embedded within HTML Page as Text

    =?Utf-8?B?UGF1bA==?=, Nov 7, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    457
    Mark Rae [MVP]
    Nov 7, 2007
  3. Thomas Dodds

    Databind an embedded control in an embedded datagrid

    Thomas Dodds, Jul 26, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    431
    Thomas Dodds
    Jul 26, 2004
  4. Trans
    Replies:
    11
    Views:
    336
    micathom
    Sep 5, 2007
  5. Num GG
    Replies:
    2
    Views:
    411
    Num GG
    Nov 17, 2008
Loading...

Share This Page