• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Opening files in JavaScript

well, the question is simple, but I don't think there is a simple answer to it...
well, what I want to do is to have my javascript code, which will be in the body, to choose a .js file from a variable I've created, I've tried several different ways, but seam to get nothing to work... I don't know if it's possible to do it like that... but in case it is, I wan't to know how...

thanks in advance
//Grim Reaper DV
0
Grdv
Asked:
Grdv
  • 5
  • 4
  • 3
  • +2
1 Solution
 
martinagCommented:
It is impossible.
You can't use JavaScript, Java, CGI or even ASP.

BTW, I am aware that this is not what you want me to say, but it's still an answer.

Martin
0
 
martinagCommented:
Hey, wait a minute! Do you mean that you want to include the *.js file?
If so, redirect to page.htm?jsfilename
And use:
<SCRIPT LANGUAGE="JavaScript">
<!--
  if (location.search && location.search.length() != 0)
    document.write('//--></SCRIPT>\n<SCRIPT LANGUAGE="JavaScript" SRC="'+location.search.substring(1)+'">');
</SCRIPT>

If it doesn't work, try
    document.write('<SCRIPT LANGUAGE="JavaScript" SRC="'+location.search.substring(1)+'"></SCRIPT>');
instead of
    document.write('//--></SCRIPT>\n<SCRIPT LANGUAGE="JavaScript" SRC="'+location.search.substring(1)+'">');
and add // --> before the </SCRIPT>

Martin
0
 
GrdvAuthor Commented:
well, first of all I as you said don't like the answer, and then about the comment, I think I've tried that in all possible ways, and I can't get it to work...
first it says unterminated literal or something... the </script> seams to be causing that problem...
and then i rewrote it like '<'+'/'+'script>' resulting the same thing, without getting that trouble... but then everything went wrong... please help... somebody...
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
martinagCommented:
Are you interested in a Perl solution?
Then would page.pl?jsfile output the HTML.

Martin
0
 
Michel PlungjanIT ExpertCommented:
1. Be sure to avoid any comments since they will make the script unstable and are not necessary since we already have established that the browser supports javascript...

I would use frames and document.write in another (hidden if necessary) frame:
<SCRIPT LANGUAGE="JavaScript"><!-- // cloak
Text = '<SCR' + 'PT SRC="' + .... + '"></SCR'+'PT>';
top.hiddenframe.document.open();
top.hiddenframe.document.write(Text);
top.hiddenframe.document.close();
// uncloak --></SCRIPT>

Anyway! If we need to use another frame, why not just load the script in a normal HTML page in that frame making the lost compatible with JS1.0 browsers too...

The perl solution also sounds good...

Michel
0
 
GrdvAuthor Commented:
well, so I guess my question will not get answered the way I wanted to... :)
well, I have thought about those different sollutions and I decided to do it in a completely different way.
should have mentioned this before, but what it really is, is a page, which is supposed to show different contents depending on how it's called, cause of the size of the document I don't want to load it everytime the user change pages.
I also though abouut using ordinary frames, but that woun't work in my case...
so what I decided to do was to split the index page, where the javascript src should have been and saved them as upper.html and lower.html
and then designed every page like this:
<--#include file="upper.html"-->
whatever the special code was
<--#include file="lower.html"-->
this way I got rid of the enourmous loading times and I didn't have to write a fraction of the code I would have been forced to do otherwise...
the perl sollution is also very good... I was considering that very much, but decided I should wait with perl until my own knowledge in the language got as good as it needed to be. (I'm learning the language you see)
So, lets make a great change of direction... the one who can help me find the best book or perl learning tutorial or whatever, will recieve the points....
preferably an online one as my economic state is not so good that I can spend about one months salary on a book...
//Grdv
0
 
QuixoteCommented:
Sorry this isn't going in the *new* direction, but I wanted to chime in on two points:


According to spec, anything javascript writes using document.write takes the place of the script that is doing the writing.  So including an ending script tag, comment tags or anything of the like will not help.  I haven't tried this, but your technique should be written as:

<script>
<!--
var JSsrc = "something";
document.write('<script src="' + JSsrc + '.js"></script>');
// -->
</script>


Now, the src= of a script tag is only accessed in Netscape 3+ and IE4 (and some IE3 if they have the javascript addition thingy).  If you want something that only works in NN3/4 and IE4 (on some platforms), then you can use a thing called Javascript entities.  A javascript variable replaces the html-escaped entity, like so:


<script>
<!--

var Src = "something.js";

// -->
</script>

<script src="&{Src};"></script>


The value you give the variable Src replaces the entity &{Src};

Works like a charm, but I don't know its full compatibility.  Introduced in Netscape 3.  That sounds like it may be exactly what you want, though.  I used it for demonstrations before (all run on NN4.05) for the exact purpose you are describing.

Let me know if this is an example of the right answer too late, or if it just doesn't work for you.

Cheers.
-Quixote
0
 
GrdvAuthor Commented:
thanks quixote, seam really great this... haven't tested it yet... :) will do tomorrow...
but it sure looks interesting... I'll have to check somewhere with compability though, because I need Netscape 3 and above and IE4 and above...
hmm... IE3 is already lost as the page uses dynamic images...
//Grdv
0
 
kollegovCommented:
Quixote, this wouldn't work :(


<script>
<!--
     var JSsrc = "something";
     document.write('<script src="' + JSsrc + '.js"></script>');
// -->
</script>

Browsers screws up on <SCRIPT even inside string constant...
You should do it so:

<script>
<!--
var JSsrc = "something";
document.write('<sc'+'ript src="' + JSsrc + '.js"></scr'+'ipt>');
// -->
</script>

0
 
QuixoteCommented:
Kollegov-

You make a good point.  That's the solution I didn't like anyway, since I don't think it would work in most browsers, which interpret all scripts prior to executing any of them.  Thus, any <script> tags written by javascript ought not to work.

I fall back on the JavaScript entities, which I know do work and have used them in this manner, but I worry about IE4.

Here's the mention of it at Netscape:
http://developer1.netscape.com:80/docs/manuals/communicator/jsguide4/getstart.htm#1009605


-Quixote
0
 
QuixoteCommented:
Kollegov-

You make a good point.  That's the solution I didn't like anyway, since I don't think it would work in most browsers, which interpret all scripts prior to executing any of them.  Thus, any <script> tags written by javascript ought not to work.

I fall back on the JavaScript entities, which I know do work and have used them in this manner, but I worry about IE4.

Here's the mention of it at Netscape:
http://developer1.netscape.com:80/docs/manuals/communicator/jsguide4/getstart.htm#1009605


-Quixote
0
 
kollegovCommented:
I used above mentioned way to write scripts in documents
created on-fly and it works perfectly in all browsers :)
But you write, never can be sure for each browser and each
situation :)


0
 
GrdvAuthor Commented:
is there anyone out there who can tell me what browsers which are compatible with this method...
//Grdv
0
 
kollegovCommented:
This will work in NN3,NN4, MIE4
(MIE3 dont support js files itself :)

<script>
     <!--
     var JSsrc = "something";
     document.write('<sc'+'ript src="' + JSsrc + '.js"></scr'+'ipt>');


----------------
This way for NN3,NN4 only
<script>
 <!--
  var Src = "something.js";
 // -->
</script>

 <script src="&{Src};"></script>

// -->
</script>
0
 
GrdvAuthor Commented:
thanks kollegov...
if you would be kind enough to just answer the question I will award you the points...
0
 
kollegovCommented:
Thanks, here it is:

This will work in NN3,NN4, MIE4
     (MIE3 dont support js files itself :)

     <script>
          <!--
          var JSsrc = "something";
          document.write('<sc'+'ript src="' + JSsrc + '.js"></scr'+'ipt>');


     ----------------
     This way for NN3,NN4 only
     <script>
      <!--
       var Src = "something.js";
      // -->
     </script>

      <script src="&{Src};"></script>

     // -->
     </script>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now