Skip navigation
Currently Being Moderated

Problems exporting to external js file

Mar 9, 2013 10:57 AM

Tags: #external_js_file #exporting_js_file

I wanted to keep my pages as clutter free as possible and also be validated so  wanted to move as much javascript to an external file.

 

I followed the instructions in this discussion http://forums.adobe.com/thread/481537?start=0&tstart=0

 

to the letter and exported most of the javascript in the template into an external file but as soon as I did that DW said that there were now syntax errors in the code in the external js file which previously worked perfectly while in the head of the template. When I deleted the javascript with the offending line of code DW pointed out further syntax errors including Google analytics code! As well as off the rack script that I had copied and pasted for putting social media buttons on the page.

 

Needless to say that when I linked the .js to my template they didn't work. But when I put them back in the head they worked perfectly.

 

What am I doing wrong? Here is the javascript. The emphasized bold italics are the places that DW says are syntax errors.

 

 

 

// JavaScript Document

 

<!--Bread crumbs code-->

 

<script type="text/javascript">

function breadCrumbs(base,delStr,defp,cStyle,tStyle,dStyle,nl) { // by Paul Davis - http://www.kaosweaver.com

loc=window.location.toString();subs=loc.substr(loc.indexOf(base)+base. length+1).split("/");

document.write('<a href="'+getLoc(subs.length-1)+defp+'" class="+cStyle+">Home</a>  '+'<span class="+dStyle+">'+delStr+'</span> ');

a=(loc.indexOf(defp)==-1)?1:2;for (i=0;i<(subs.length-a);i++) { subs[i]=makeCaps(unescape(subs[i]));

document.write('<a href="'+getLoc(subs.length-i-2)+defp+'" class="+cStyle+">'+subs[i]+'</a>  '+'<span class="+dStyle+">'+delStr+'</span> ');}

if (nl==1) document.write("<br>");document.write('<span class="+tStyle+">'+document.title+'</span>');

}

function makeCaps(a) {

  g=a.split(' ');for (l=0;l<g.length;l++) g[l]=g[l].toUpperCase().slice(0,1)+g[l].slice(1);

  return g.join(" ");

}

function getLoc(c) {

  var d="";if (c>0) for (k=0;k<c;k++) d=d+"../"; return d;

}

</script>

 

 

<!--Google analytics code-->

 

<script type="text/javascript">

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");

document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

</script>

 

<script type="text/javascript">

try {

var pageTracker = _gat._getTracker("UA-8313452-1");

pageTracker._trackPageview();

} catch(err) {}</script>

 

 

 

<!--Script for adding Social Media Share Buttons-->       

 

<script type="text/javascript">var switchTo5x=true;</script>

<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>

<script type="text/javascript">stLight.options({publisher: "380208c3-e9f3-4198-8bef-1de3e344f3ae", doNotHash: true, doNotCopy: true, hashAddressBar: false});</script>

 
Replies
  • Currently Being Moderated
    Mar 9, 2013 11:10 AM   in reply to VL Branko

    No script tags required in JS files.  All you need is the relevant code.

     

    Some functions need to be inside the HTML document or they won't invoke on page load.  I typically put them inside a server-side include.

     

     

     

    Nancy O.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2013 1:52 PM   in reply to VL Branko

    I disagree about SSIs.  Been using them for years without any security issues on Linux/Apache servers.  However some ASP aficionados no longer advocate their use favoring other ASP code instead.   But that's a topic for a different discussion as I don't use ASP or ASP.net enabled servers.

     

     

    Nancy O.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2013 1:59 PM   in reply to VL Branko

    Your system administrator is throwing the baby out with the bath water. SSI, when used as a way of consolidating changeable code into a single file is not a significant security risk. Use Google to read about the scope of this problem. Then give your IT friend a slap upside the head!

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2013 6:46 AM   in reply to VL Branko

    VL Branko wrote:

     

    Thank you Nancy that solved the problem of syntax errors but as you ruefully remarked other conditions are necessary for them to work and in this case sitting in an external js file is not sufficient and I had to put them back in the head for them to work. Oh well. The only problem is now the page wont validate because the validator thinks that the js in the breadcrumb script has errors in it. May have to go for a different one and see if that validates.

     

    You can avoid that by encapsing the javascript in CDATA markup. Google it to find out the details.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points