Link to home
Start Free TrialLog in
Avatar of Grdv
Grdv

asked on

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
Avatar of martinag
martinag

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
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
Avatar of Grdv

ASKER

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...
Are you interested in a Perl solution?
Then would page.pl?jsfile output the HTML.

Martin
Avatar of Michel Plungjan
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
Avatar of Grdv

ASKER

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
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
Avatar of Grdv

ASKER

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

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


Avatar of Grdv

ASKER

is there anyone out there who can tell me what browsers which are compatible with this method...
//Grdv
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>
Avatar of Grdv

ASKER

thanks kollegov...
if you would be kind enough to just answer the question I will award you the points...
ASKER CERTIFIED SOLUTION
Avatar of kollegov
kollegov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial