Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 10th, 2004, 10:17 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default Advice on Dynamically assigning external js file

I have a web page that has the following code...

    switch (optwizsel)
    {
        case "YYP":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard1.js'></scr" + "ipt>");
        break;
        case "YNP":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard2.js'></scr" + "ipt>");
        break;
        case "NYP":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard3.js'></scr" + "ipt>");
        break;
        case "NNP":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard4.js'></scr" + "ipt>");
        break;
        case "YYY":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard5.js'></scr" + "ipt>");
        break;
        case "YNY":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard6.js'></scr" + "ipt>");
        break;
        case "NYY":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard7.js'></scr" + "ipt>");
        break;
        case "NNY":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard8.js'></scr" + "ipt>");
        break;
        case "YYN":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard5.js'></scr" + "ipt>");
        break;
        case "YNN":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard6.js'></scr" + "ipt>");
        break;
        case "NYN":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard7.js'></scr" + "ipt>");
        break;
        case "NNN":
            window.document.write("<scr" + "ipt language='JavaScript' src='setup/alerts/alertwizard8.js'></scr" + "ipt>");
        break;
        default:
        break;
    }

Basically all it does is go through a routine of checking a unix server for the config of certain items and based upon that config writes the appropriate statement in the head to assign the correct external js file.

Here is my question and the advice I am needing...This has the high probability of expanding in the future beyond checking the three config options I am checking today. My concern is complexity and size of the page. Ideally what I would love to do is assign an external js file that simply does the checking and writes the appropriate code line to assign the correct js file to the page. Is this possible? Is it possible to have an external js file that writes to the head an external js file?

And if anyone has any other ideas, I am open to them.

Clay Hess
__________________
Clay Hess
  #2 (permalink)  
Old June 10th, 2004, 10:56 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I don't think you can do this, but I would suggest a server language like PHP, ASP or CGI if it works with you.

HTH,

Snib

<><
  #3 (permalink)  
Old June 10th, 2004, 11:40 AM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Snib,

Thanks for the advice. Unfortunately, while I would love to use ASP or something of that nature, in this case we cannot. The unix server I am communicating with is not a web server. It is simply a server our company sells with our own custom programs on them and I interact with it. So a server side language is out of the question right now.

Clay Hess
  #4 (permalink)  
Old June 10th, 2004, 11:48 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I have another idea: can you make the page a .shtml and then use an #include statement to include your JavaScript from a separate file?

Snib

<><
  #5 (permalink)  
Old June 10th, 2004, 01:07 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Snib,

Usually when I am including files and such from a server side, I am using ASP. So I am not as familiar with shtml and the ramifications so let me ask you some things...

This webpage is being hosted locally on the users harddrive. Can shtml and the include code reference a local file and not be server side at all? Basically, the only interaction I have with the unix server is getting config info in a boolean format. SO do you think that would work?

Clay Hess
  #6 (permalink)  
Old June 10th, 2004, 01:57 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hmm... I can't get it to work locally, but it would with IIS or Apache installed locally. I know you don't have either on the Unix box, and I'm guessing you don't have them locally, either.

Hope you solve this!

Snib

<><
  #7 (permalink)  
Old June 10th, 2004, 03:00 PM
Friend of Wrox
 
Join Date: Apr 2004
Location: Wausau, WI, USA.
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have IIS on my machine but I of course cannot control what is on the users machine. So that would not work. Let me know if inspiration strikes you again.

Clay Hess
  #8 (permalink)  
Old June 10th, 2004, 03:36 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

How about shuffling your files a bit around, renaming them, and then use this code:

window.document.write("<scr" + "ipt type='text/javascript' src='setup/alerts/alertwizard' + optwizsel + '.js'></scr" + "ipt>");

Endless possibilities. Just change the value of optwizsel and add more .js files.....

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #9 (permalink)  
Old June 10th, 2004, 03:42 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Yeah, that's a good idea. Something like this:

function IS(num) //IS = include script
{
 document.write("<scr" + "ipt type='text/javascript' src='setup/alerts/alertwizard' + num + '.js'></scr" + "ipt>");
}

switch(optwizsel)
{
 case "NNN" : IS(8); break;
}

That would greatly reduce the size of your page.

Snib

<><
  #10 (permalink)  
Old June 10th, 2004, 05:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: High Wycombe, UK, United Kingdom.
Posts: 344
Thanks: 0
Thanked 1 Time in 1 Post
Default

There is some examples of different methods at http://www.codehouse.com/javascript/articles/external/ which cover this and may be of assistance.


Similar Threads
Thread Thread Starter Forum Replies Last Post
External JS files Ben Horne Javascript 3 July 25th, 2008 02:00 AM
dynamically link js file darkhalf Javascript 3 September 15th, 2006 09:05 AM
Thoughts on Dynamically assigning external js file fs22 Javascript 1 June 23rd, 2004 02:12 PM
refreshing external js file fs22 Javascript How-To 1 June 17th, 2004 05:02 PM
Dynamically assign external js file fs22 Javascript How-To 1 May 21st, 2004 05:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.