View Full Version : Reloading an external js library?
NZ_Tango
11-17-2002, 11:25 PM
Currently on one of may pages I'm loading an external JS file using an asp page, eg. <SCRIPT src="http://www.somehost.com/js.asp?param=something"></SCRIPT>.
The JS code that the ASP page returns, is often changing, so is there a way to reload the extenral script file without reloading the whole page?
Any help appreciated, cheers.
chrismiceli
11-18-2002, 01:33 AM
never tried it, but you could try and do this
document.script.src = "src";
try it, you never know.
<edit> you might want to try giving the script tag a name like this <script src="s.js" type="text/javascript" name="hello">
and the change it like this
document.hello.src = "s.js"
NZ_Tango
11-18-2002, 04:06 AM
Yup tried that to no avail. Even tried putting it in a div, then swapping the DIV innerHTML, but it didn't load new library. There must be a way.
beetle
11-18-2002, 04:13 AM
Maybe load them all then set disabled equal to true, and then using JS to disable/enable those that are needed (like a stylesheet switcher)
???
whammy
11-18-2002, 05:04 AM
I'd be more interested as to what you're trying to accomplish with the ASP page in general, as this doesn't seem like a logical approach. :)
Can you explain in more detail the general scope of the application?
You might be better off including javascript in a server-side subroutine depending on what is displayed on the page (or including the file in the subroutine).
An example of including an outside file (as an .asp file) depending upon a condition:
<%
If HelloWorld = True
%>
<!-- #include file = "helloworld_js.asp" -->
<%
End If
%>
Either way, it should make a lot more sense with ASP that way.
NZ_Tango
11-18-2002, 05:36 AM
What I was trying to achieve is a small chat box on my website, lets just say each line of chat is stored in a text file on the server (we wont worry about how they get there), which is read by my server side code and returned via the external script by using document.write, so basically the chat in the text file is displayed on the site.
Now obviously when the content of the text file changes, it doesn't change on the website, unless you reload the page. I was hoping to be able to just reload the script say every 10 seconds, which updates the chat :)
I would be able to achieve this using an Iframe and reloading it, but I'm not keen on that approach for a various reasons.
whammy
11-18-2002, 11:35 PM
Well, you won't be able to write to the text file with javascript. If you were using ASP/VBScript, it's rather easy...
http://www.solidscripts.com/displayscript.asp?sid=7
NZ_Tango
11-18-2002, 11:45 PM
I have a method sorted for writing to the text file, but guess I might have to go the Iframe refreshing way to display it.
whammy
11-19-2002, 12:42 AM
How are you writing to a text file on a remote server using a client-side language?
Please enlighten us. ;)
Vladdy
11-19-2002, 01:09 AM
DOM2 Compliant method:
newScript=document.createElement('script');
target='js.asp?Param=Whatever';
newScript.src=target;
document.getElementsByTagName('head')[0].appendChild(newScript);
whammy
11-19-2002, 01:11 AM
That will write to the server using javascript on the client-side?
If that is true, I see all kinds of security problems with that already... but I don't know of many browsers that are DOM2 compliant anyway. ;)
Vladdy
11-19-2002, 01:13 AM
It will dynamically load a script file. Works with IE5+ and all Gecko based browsers (>90% of users and growing)
whammy
11-19-2002, 01:16 AM
That's cool, but you still can't write to a remote server using javascript... sorry if I'm confusing the issue, but I would like to know how he's going to write to the text file without using a server-side language. :)
As far as I know, it is currently impossible.
Vladdy
11-19-2002, 01:17 AM
Server-side JScript, you can :D :D :D
My impression is that he meant he can write to the server (submit a form) without reloading the page.
whammy
11-19-2002, 01:18 AM
Well, duh. ;)
NZ_Tango
11-19-2002, 01:18 AM
Originally posted by whammy
How are you writing to a text file on a remote server using a client-side language?
Please enlighten us. ;)
Well I havn't yet, but when I figure out how to reload the JS file, I will simply pass the new comment to the server in the script URL that I call :) I've already used this method while loading XML documents and it works well.
Thanks heaps Vladdy, will give that a go.
whammy
11-19-2002, 01:19 AM
And how are you going to save the information to the server?
You really need to use ASP, PHP, ASP.NET, Perl, or whatever... you just can't do this with client-side scripting. It's not that "simple". :)
P.S. Does your webhost support any server-side scripting languages? It looks like you'll need one for what you're trying to do!
I'd suggest PHP or ASP to begin with. They are the easiest to learn.
NZ_Tango
11-19-2002, 01:21 AM
Yes I am using ASP, and will simply use Request("comment") or something and then save it to the text file.
whammy
11-19-2002, 01:22 AM
Then don't use javascript...
You should use Scripting.FileSystemObject instead. :)
http://www.w3schools.com/asp
Trust me, I am an ASP web developer. If you aren't going to use a database, then use FSO, and the link I posted above will teach you everything almost that you need to know. :D
NZ_Tango
11-19-2002, 01:23 AM
LOL thats exactly what I am using.
I'll give you the URL once I have it working so ou can see it in action, and know why I need to use javascript.
whammy
11-19-2002, 01:24 AM
Cool, you should post in the ASP forum in the future then, since this is the "javascript" forum. :)
I apologize, because as the posts progressed, it seems like you were getting further and further away from what you're trying to do.
How 'bout you delete this thread and post a new one in the ASP forum with a clear explanation, since we know you're using ASP? :)
Vladdy
11-19-2002, 01:27 AM
Have a code like that in your html document:
function deleteScriptNode()
{ document.getElementsByTagName('head')[0].removeChild(newScript);
return;
}
and make the last statement of the script that you load periodically:
setTimeout("deleteScriptNode()", 100);
Otherwise you end up with too many script nodes in your <head>
:D :D :D :D
NZ_Tango
11-19-2002, 01:27 AM
But I had the ASP part of it worked out fine. My problem was the javascript part - reloading an external JS library.
Thanks to Vladdy I've got it working now, so I will start building the application. Hehe thanks for that last addition, saw it after I posted.
whammy
11-19-2002, 01:31 AM
Cool... I guess you lost me then. I use javascript all the time in my server-side applications.
I guess I wouldn't be confused, but I still don't get what you're trying to do (I don't understand why you want to "reload" your external javascript file), and you haven't explained why you want to do this.
If that's the case I still think you'd be better off using server-side scripting and loading a different script depending on the situation... (using subroutines)
Please accept my apologies, since I am trying to help you out. Can you explain why this is not an option?
:D
NZ_Tango
11-19-2002, 01:39 AM
Hehe no problem, I'll explain it a little better.
1) You load the webpage, and you see the chat contents of the text file, which has been included from the server.
2) After 5 seconds, I want the contents of the chat box to update, to show the new contents of the text file (on the server), as people have been adding messages. I want this to happen without reloading the page.
So I reload a new script periodically, which calls an ASP page, and returns client side javascript that re-writes the content of the box on the page. So you have a live chat without reloading the page.
3) When people type in a new message and send it, it reloads the script again, and passes in the comment in the URL. eg. chat.asp?comment=Hello World. The server grabs this comment, adds it to the chat file, then returns the javascript to re-write the contents of the chat box again.
Hope thas a little clearer :D
NZ_Tango
11-19-2002, 01:40 AM
OK I may have missed one of your points. Are you suggesting its possible to call a server side function from the client, without reloading the page?
whammy
11-19-2002, 01:46 AM
You have to reload a client-side page in order to get any information from the server. Now we're getting somewhere. :)
If you want to hide the reloading, perhaps you could use javascript in combination with a hidden frame that requests information from the server, so the user doesn't notice it, and then you can update the "displayed" page (the client's "parent" frame).
No matter what, you have to refresh something in order for the client's webpage to update with information from the server. But if you use a hidden frame they don't have to see it. :)
beetle
11-19-2002, 01:48 AM
Server-side JScript....right that's it! Now try to find the 3 people that use it for advice...;)
Just kidding....
NZ_Tango
11-19-2002, 01:49 AM
Originally posted by whammy
[B]You have to reload a client-side page in order to get any information from the server.
Ahhh but I disagree, because my method works :p
Guess I'll just need to show you eh?
whammy
11-19-2002, 01:49 AM
LOL... I have used server-side JScript in quite a few situations. Sometimes it's a lot less code than VBScript in ASP, although usually the reverse is true. :)
whammy
11-19-2002, 01:51 AM
Well, if you can show me WHAT works, and what ISN'T working, I may be able to solve your problem lol.
NZ_Tango
11-19-2002, 01:51 AM
My entire site uses server side JScript :D Have a look if you want http://rsnz.m6.net
I havn't had a chance to build the chat cos I keep replying here lol, it all works, I have no problems now, so I will show you when it is done.
whammy
11-19-2002, 01:54 AM
Ok, first of all you need to delete this post (since you posted in the wrong forum entirely), and to avoid confusion.
Then, you need to repost your question in the ASP forum (noting that you use JScript), with a clear explanation of your problem.
:D
If you no longer have any problems, then just delete the post to save bandwidth, LOL.
I know you wasted more than my time...
NZ_Tango
11-19-2002, 01:56 AM
LOL I give up, I posted here becasue its a javascript question (client side), which Vladdy solved for me.
I kept posting to help you understand my application, becasue you were asking.
whammy
11-19-2002, 02:01 AM
Ok, so perhaps I am just an idiot. :)
client-side javascript is not my forte (although I have a pretty good grasp of the basics (and more)), I apologize if I confused anyone other than myself. :)
You can shoot me now. But I bet I can still help you sometime. :D
I wasn't aware you could reload a client-side javascript without refreshing something either. :confused:
And I will bet you $1000 that it won't work in all browsers (I don't care about NS 4.x anymore), that's one reason I don't bother with this kind of thing.
NZ_Tango
11-19-2002, 02:04 AM
I wasn't aware you could reload a client-side javascript without refreshing something either.
Its not something I have seen done before, this is pioneering stuff :D
Browser wise, I'll only make it show up in those that support it.
whammy
11-19-2002, 02:23 AM
Have fun! I hate the whole "browser compatibility" issue.
That's one reason I'm using ASP and learning ASP.NET. Until there are true standards, anything else is a waste of my time, and yours.
I am a big supporter of XHTML, XML, etc. and I hope to see that trend continuing.
Perhaps you lost me because I don't like to learn technologies that may not be around in 6 months. :D
If I'm wrong, gimme some info on what you're doing. :D
NZ_Tango
11-19-2002, 02:50 AM
Hehe I'm with you on that one, I do pretty much everything server side because I hate the whole browser compatibilty thing.
Its just this app will require it, and besides the code is DOM2 compliant, so it should be around for a while.
Vladdy
11-19-2002, 06:16 PM
One more thing:
DO NOT forget to put
Response.ContentType="text/JavaScript"
in your asp file that returns js.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.