PDA

View Full Version : Ajax Includes Script - external instead of in Head?



Andrew
11-23-2005, 05:50 PM
Ajax Includes Script.
http://www.dynamicdrive.com/dynamicindex17/ajaxincludes.htm

Hi. I was wondering, would it be possible to use the Ajax Includes Script
as an external file instead of it being repeated in the Head of each page?

Twey
11-23-2005, 05:59 PM
Of course.

Andrew
11-23-2005, 06:02 PM
Heh, thanks. Maybe I should have added... how? :) I've spent a couple of hours experimenting with it but without any luck.

Twey
11-23-2005, 06:36 PM
Just put the bit it says to paste into a seperate file - let's call it "ajax.js" - and include it where you would normally paste the script with the tag:
<script src="ajax.js" type="text/javascript"></script>This assumes that "ajax.js" is in the same directory as the page, of course.

Andrew
11-23-2005, 07:39 PM
Hmm, the files I'm experimenting with are...

Index.html
Template.html
ajax.js

...and they're all in the same directory. With the full script in the Head of the Index page it works great, but when the script is removed and replaced with a link to the external file the contents of the Template page are no longer included. :confused:

Twey
11-23-2005, 07:54 PM
That's strange... have you checked it's being included properly?
Run
javascript:window.alert(typeof ajaxinclude == "function" ? "Script was included properly." : "Script wasn't included!");in the address bar of your browser whilst viewing your page.

Andrew
11-23-2005, 08:10 PM
That returns "Script wasn't included". It could just be that I'm overlooking something obvious. :)

Twey
11-23-2005, 08:29 PM
I think it may be. Can you paste all your <head> section here (in [code] tags, please)?

Andrew
11-23-2005, 08:45 PM
Certainly...



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>

<HEAD>

<TITLE>Testpage</TITLE>

<script src="ajax.js" type="text/javascript"></script>

</HEAD>

Viewed online, it gives an error:

Line: 33
Char: 13
Error: Object Expected
Code: 0

Twey
11-23-2005, 09:19 PM
Hm... nothing wrong there. You say it's live? Would you mind linking?

Andrew
11-23-2005, 09:25 PM
Sure - the links are good for 24 hours:

Index.html (http://trialsites.ihoststudio.com/trialsite59016/website/)
template.html (http://trialsites.ihoststudio.com/trialsite59016/website/template.html)
ajax.js (http://trialsites.ihoststudio.com/trialsite59016/website/ajax.js)

I tried removing <script type="text/javascript"> and </script> from ajax.js following something I read elsewhere but it made no difference.

Twey
11-24-2005, 04:25 PM
For one, yes, you shouldn't have the <script></script> HTML tags in the JS file. That's the only error I get - Firefox stops including the script there. Can you remove them so I can see the rest?

Andrew
11-24-2005, 05:39 PM
A-ha, that's it! :) Hmm, it beats me what I was doing differently yesterday, but I just removed the <script> tags again and now it's working fine. :rolleyes:

Thanks a lot, Twey! ;)