...

View Full Version : Differences between linked scripts and code in script tags...



]|V|[agnus
06-10-2004, 03:11 PM
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?

requestcode
06-10-2004, 03:21 PM
There should not be any differences between an external javascript and one placed directly in the document. They are both treated the same.

jbot
06-10-2004, 03:34 PM
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.

]|V|[agnus
06-10-2004, 03:53 PM
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:



<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?

jbot
06-10-2004, 03:57 PM
|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:


<script type="text/javascript" src="url.cfml"></script>

kapiche?

liorean
06-10-2004, 04:09 PM
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.

]|V|[agnus
06-10-2004, 04:18 PM
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:



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

glenngv
06-11-2004, 06:44 AM
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.


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


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

]|V|[agnus
06-11-2004, 03:21 PM
Interesting. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum