PDA

View Full Version : Linking an external JS file, not working??



jack_981
Sep 27th, 2010, 12:50 PM
Hey guys, just wondering why this isn't working.

My company is a small advertising business, with two of us in the IT department, we aren't fully skilled in javascript or most other languages, but we do know HTML & CSS.
The website that we use for advertising is http://www.hotelsukonline.com. keep in mind this is not a complete website :o

But this is just stupid...

We have a Javascript 'accordian' to view a couple of adverts next to a google map on our hotel listings


<iframe id="map-width" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="google map source"></iframe>


<div id="business-listings">
<script src="../adverts.js" type="text/javascript"></script>

</div>


the javascript is just a simple document.write that writes a load of divs out.


document.write( <div id="AccordionContainer" class="AccordionContainer">
<div onclick="runAccordion(1);">
<div class="AccordionTitle" onselectstart="return false;">
> Restaurants in Leamington Spa
</div>
</div>
<div id="Accordion1Content" class="AccordionContent">
<ul class="advert-names">

</ul>
</div>

<div onclick="runAccordion(2);">
<div class="AccordionTitle" onselectstart="return false;">
> Pubs/Bars in Leamington Spa
</div>
</div>
<div id="Accordion2Content" class="AccordionContent">
<ul class="advert-names">
</ul>
</div>
</div>
);

On some pages it shows up, but on a few they dont.
From what we can see its the same thing in each of them, we've copy and pasted code from an exisiting, working page into a broken page for it not to work.. we've had it before but found a work around to it. This time we just can't see it...


Please someone help us... Have had nothing but earache for a while about this now.

Sorry if we're just being stupid and missed out something really simple.


P.S

Forgot to mention that it works perfectly on every page in FF, but in everything else it doesn't.

abduraooft
Sep 27th, 2010, 12:53 PM
You need to enclose strings inside quotes, like

document.write( '<div id="AccordionContainer" class="AccordionContainer">
<div onclick="runAccordion(1);">
<div class="AccordionTitle" onselectstart="return false;">
> Restaurants in Leamington Spa
</div>
</div>
<div id="Accordion1Content" class="AccordionContent">
<ul class="advert-names">

</ul>
</div>

<div onclick="runAccordion(2);">
<div class="AccordionTitle" onselectstart="return false;">
> Pubs/Bars in Leamington Spa
</div>
</div>
<div id="Accordion2Content" class="AccordionContent">
<ul class="advert-names">
</ul>
</div>
</div>');

jack_981
Sep 27th, 2010, 12:59 PM
You need to enclose strings inside quotes, like



Thanks for the quick reply, but that doesn't work, it comes up with a syntax error in DW and doesnt show if I upload it.

Thats the first thing I thought of but no luck :(

abduraooft
Sep 27th, 2010, 01:16 PM
Thanks for the quick reply, but that doesn't work, it comes up with a syntax error in DW and doesnt show if I upload it.
Sorry, you need to escape the new lines in the string, like

document.write( '<div id="AccordionContainer" class="AccordionContainer">\
<div onclick="runAccordion(1);">\
<div class="AccordionTitle" onselectstart="return false;">\
> Restaurants in Leamington Spa\
</div>\
</div>\
<div id="Accordion1Content" class="AccordionContent">\
<ul class="advert-names">\

</ul>\
</div>\

<div onclick="runAccordion(2);">\
<div class="AccordionTitle" onselectstart="return false;">\
> Pubs/Bars in Leamington Spa\
</div>\
</div>\
<div id="Accordion2Content" class="AccordionContent">\
<ul class="advert-names">\
</ul>\
</div>\
</div>'); Or make remove all newlines and put them all in a single line.

PS: Why do you need to use unreliable javascript to output html? Can't you use some serverside code like PHP?

djh101
Sep 27th, 2010, 01:18 PM
If your server supports any server side languages (which I don't know why it wouldn't), file including I think would be a lot more reliable than writing from a JavaScript file- there doesn't seem to be much JavaScript in your script, anyway. If you do want to keep it as it is, though, quotes are what you need around your content. Also, try removing the linebreaks; JavaScript doesn't like linebreaks.

jack_981
Sep 27th, 2010, 01:21 PM
PS: Why do you need to use unreliable javascript to output html? Can't you use some serverside code like PHP?

we could but we don't really understand PHP.. it's a little embarrassing but it's true

djh101
Sep 27th, 2010, 01:26 PM
I actually spent quite a while knowing nothing about php except the include(); command. If you're going to only know one command, though, that would be it. Before I learned that, I was using JavaScript for my external navigation bar.

<?php include("myfile.html"); ?>

jack_981
Sep 27th, 2010, 01:29 PM
I actually spent quite a while knowing nothing about php except the include(); command. If you're going to only know one command, though, that would be it. Before I learned that, I was using JavaScript for my external navigation bar.

<?php include("myfile.html"); ?>


would myfile.html just be in the same folder as where my JS file is being held at the moment? or would i have to do anything with Mysql... it's always confused me.

abduraooft
Sep 27th, 2010, 01:35 PM
would myfile.html just be in the same folder as where my JS file is being held at the moment? No, to work the above php line, your file myfile.html need to be in the same folder as that of the file that is including. You may also specify the correct path to the file, if it's in a different folder.