Solved

Opening files in JavaScript

Posted on 1998-11-15
16
246 Views
Last Modified: 2011-09-20
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
Comment
Question by:Grdv
  • 5
  • 4
  • 3
  • +2
16 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1274009
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
 
LVL 4

Expert Comment

by:martinag
ID: 1274010
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
 
LVL 2

Author Comment

by:Grdv
ID: 1274011
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
 
LVL 4

Expert Comment

by:martinag
ID: 1274012
Are you interested in a Perl solution?
Then would page.pl?jsfile output the HTML.

Martin
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 1274013
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
 
LVL 2

Author Comment

by:Grdv
ID: 1274014
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
 
LVL 2

Expert Comment

by:Quixote
ID: 1274015
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
 
LVL 2

Author Comment

by:Grdv
ID: 1274016
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 10

Expert Comment

by:kollegov
ID: 1274017
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
 
LVL 2

Expert Comment

by:Quixote
ID: 1274018
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
 
LVL 2

Expert Comment

by:Quixote
ID: 1274019
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
 
LVL 10

Expert Comment

by:kollegov
ID: 1274020
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
 
LVL 2

Author Comment

by:Grdv
ID: 1274021
is there anyone out there who can tell me what browsers which are compatible with this method...
//Grdv
0
 
LVL 10

Expert Comment

by:kollegov
ID: 1274022
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
 
LVL 2

Author Comment

by:Grdv
ID: 1274023
thanks kollegov...
if you would be kind enough to just answer the question I will award you the points...
0
 
LVL 10

Accepted Solution

by:
kollegov earned 220 total points
ID: 1274024
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now