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.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Differences between linked scripts and code in script tags...

    If I link a script like so:

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

    How can I supply variables in that script values from, say, ColdFusion? I have no problem doing so if the script is between the script tags sans the "src" attribute, but as the script is somewhat lengthy, and I love minimalism, it annoys me to have its full guts and glory displayed.

    Do I have any options, or is this a limitation of linking scripts in that manner?

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There should not be any differences between an external javascript and one placed directly in the document. They are both treated the same.

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, but he means calling a JS file with the .cfml extension as opposed to the usual .js one, since he wants to parse serverside variables directly into the JS.

    Now, you can do this. but, depending on server, you might have to alter some settings re Mime-types to allow this.

  • #4
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, that is not what I am saying. The file has the .js extension. Perhaps lack of specifics confused the issue initially.

    The script is a script for preloading a collection of images while displaying a positioned progress par while doing so. The list of images to be preloaded can be entered one by one, or of course passed with a javascript variable, but my list of images is coming from a server-side directory query. It's the list of contents of a particular images directory. So that line of javascript looks like so:

    Code:
    <CFoutput>
    var imagenames=new Array(#preloads#);
    </CFoutput>
    <CFoutput>, for those that aren't familiar, is the necessary container for outputting any coldfusion variables. (e.g. #thisIsAVariable#)

    So anyway, what I had happening was that this value was being processed if the script was entered betwixt script tags, whereas I had no luck when referencing the library with the "src" attribute. Perhaps something else was amiss though?

    Edit: I think it might be simply that the <CFoutput> doesn't get processed by the ColdFusion server because it's in the linked .js file... perhaps?

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow no, i'm still right

    Quote Originally Posted by ]|V|[agnus
    No, that is not what I am saying. The file has the .js extension. Perhaps lack of specifics confused the issue initially.

    Edit: I think it might be simply that the <CFoutput> doesn't get processed by the ColdFusion server because it's in the linked .js file... perhaps?
    that's why you give the JS file a CMFL extension, or whatever you guys use. which is wot i was saying in the first place.

    there's no way you can process a CFML file with the extension .js unless the server is set up for that, which it obviously isn't.

    your code should therefore be:

    Code:
    <script type="text/javascript" src="url.cfml"></script>
    kapiche?

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You must make two things sure:
    - The file is treated as a CF file server side.
    - The file is sent using 'application/x-javascript' or the nonregistred 'text/javascript' to the client.

    For the first one you need to make sure your file is considered a server parsed file, and that it is parsed using CF. I don't know anything about CF so I wouldn't know how to set that up, but Macromedia surely must have documentation on it.

    As for the second, you do that from within the server parsed file using a CF command.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Relax jbot, you were telling me what I ought to do by telling me what I meant, and by that choice of wording, you were wrong. But now that you explained yourself better, I understand, and that makes sense.

    THANKS. FOR. YOUR. HELP.

    And thanks for that tidbit too, liorean. At any rate then, what I came up with yesterday before posting this is an easy enough middle ground I think:

    Code:
    <script type="text/javascript">
       <CFinclude template="js/_preload.js">
    </script>
    I'd rather not rename the JS library with a .cfm extension of anything. That would annoy the obsessive side of me to no end... Though, if I can find a way to work it so I can reference it with the <script> "src" attribute, then that'd be nice so neither I, nor any users, will see the script when looking at the host page's markup.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts

    If I understood you correctly...

    Depending on how the imagenames array is accessed in the external script.
    If the external js requires the array to be defined as the js is being loaded, then you can declare the array first before loading the external script.
    Code:
    <script type="text/javascript">
    <CFoutput>
    var imagenames=new Array(#preloads#);
    </CFoutput>
    </script>
    <script type="text/javascript" src="library.js"></script>
    and then removing the var imagenames declaration in library.js.

    Otherwise, load the external script first then set the array later.
    Code:
    <script type="text/javascript" src="library.js"></script>
    <script type="text/javascript">
    <CFoutput>
    imagenames=new Array(#preloads#);
    </CFoutput>
    </script>
    then no need to make some changes to the external script.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Interesting. Thanks.


  •  

    Posting Permissions

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