Where to put a script

Hi

I had this script at the bottom of a page that called the Google Analytics and another script, which is supposed to prevent text scraping from the scripts folder.

<script type="text/javascript">
 function downloadJSAtOnload() {
 var element = document.createElement("script");
 element.src = "/Scripts/googleanalytics.js";
 document.body.appendChild(element); 
 
 var element = document.createElement("script");
     element.src = "/Scripts/text.js";
     document.body.appendChild(element);
 }
 if (window.addEventListener)
 window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
 window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;
 </script>

I changed the code for GA and thought that I could just add

<script src="Scripts/text.js></script>

but this last script does not work. How can I call it from the webpage?

Can you check that the folder containing the text.js file really does start with a capital ā€˜S’?

Hi chris

In the real thing the path is correct.

My question is: can I launch the script like that from the bottom of the page, beacuse it is not working

When you move the JS to its own file, did you leave the <script> tags out? So long as the JS code is the same, and you’ve correctly linked to the new file, then it should work.

the script is in the scripts folder. Before it worked in tamdem with the GA, in afashin that I implemented some time ago and can’ quite remember how and why.

But now that I’ve taken the GA out I thought I could just do that, I’ve tried to put it the head, and after the footer, but it no longer works.

it is not very important but I would like to understand why,

the path is correct.

~PS

Well I’ve put it back exactly as it was before, having deleted the lines for GA and it’s working again!

Doesn’t make sense!

Why should it work like this at the bottom

<script type="text/javascript">
 function downloadJSAtOnload() 
{
 var element = document.createElement("script");
     element.src = "Scripts/text.js";
     document.body.appendChild(element);
 }
 if (window.addEventListener)
 window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
 window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;
 </script>

and not like this also at the bottom

 <script src="Scripts/text.js></script>

Can you show us the full HTML of the page as it is currently?

Perhaps I should have been clearer about the content of text.js. Can you confirm that this file contains only the following, with no <script></script> tags in it? It should contain only the JavaScript code shown below.

function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "/Scripts/googleanalytics.js";
 document.body.appendChild(element); 
 
 var element = document.createElement("script");
     element.src = "/Scripts/text.js";
     document.body.appendChild(element);
 }
 if (window.addEventListener)
 window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
 window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;

We crossed posts

The script in the scripts folder is not within scripts tags and has nothing to do with GA

What I used sometime ago was a means of getting a few scripts together to speed up loading

Basically with the curly brackets I could put as many scripts as I liked (in theory…). the script in question has nothing to do with GA

{
 var element = document.createElement("script");
     element.src = "Scripts/text.js";
     document.body.appendChild(element);
 }

The code above and below this is a totally different script that is used to hold other scripts.

OK. There was a slight misunderstanding on my part as to what you were trying to achieve. Let me have another look.

One point on where to place the scripts though. The idea behind putting them at the botton of the HTML just before the closing </body> tag, is so that the rest of the page loads before you try to modify it with the JS. If you call it from the ,head>, there is no guarantee that what you are applying the JS to is actually loaded, so causing an error, or getting no result from it being there.

1 Like

It looks like that script is just being used to pull two JS files into your web page - assuming that to be an accurate interpretation, is there any reason why you don’t just put the following just before that closing <body> tag?

<script src="/scripts/googleanalytics.js"></script>
<script src="/scripts/text.js"></script>
1 Like

I knew that, but I didn’t know why. Thank you for the explanation.

Well, yes and no.

GA has changed thei code and the old one will soon be deprecated. I am changing it in all my pages and right now for reasons that I can’t quite remember … I am pasting the full code at the bottom of the page,

The text.js, well that’s exactly what I have been trying to do and why I am here now. But it doesn’t work like that even though it works if embedded in enveloping script!

I can’t think why either. Right now, it looks like you’re using JS in the HTML page to pull in two other JS files from a separate folder, and attach them to two <script> tags it has created. One thing I’m not too sure of though is where it is placing the <script> tags within the HTML structure.

Do you have a link to a working page I could look at - I’d like to see how the page source looks in Dev Tools once it has all run successfully.

let me move something online where it does not conflict with my stats. I need 5 mins.

I haven’t had my ration of caffeine yet :coffee: so I;m likely missing something, but this sounds like

ā€œafter the page has loaded, add this script that will run when the page is loadedā€

Yes, that’s what should happen.

I’m having problems and cannot undersatnd anything! I moved a page to a Temp file and eleted the GA code because I don’t want to add to the stats, and even without the code it keeps tracking… Am I seeing things?

Give a bit more time

I was working on the wrong file…I am tired!

Ok, here is one page AFTER I pasted in the new GA and left only the other script in the holding envelope script… (can’t think what to call it)

http://pintotours.net/TEMP1/BarceloDomini.html

Change that code to the following - with it running at the bottom of the page the page has already loaded so all you need is:

(function()  {
 var element = document.createElement("script");
     element.src = "Scripts/text.js";
     document.body.appendChild(element);
 }());
1 Like

Thank you felgal

i take it that will be within the script tags, right?

Anyway, what we are trying to discover is why the simple

<script src="/Pinto/scripts/text.js></script>

does not work, while what is there does.

only if embedded directly in the page

is text.js in the Scripts/ folder or in the /Pinto/scripts/ folder - obviously it isn’t in both or they’d both work. Does the folder name have a capital S or not?

Scripts and scripts are two different folders.

The thread started with invented paths, as I mentioned, hence the capital S and other things. Now you have the true paths in the html file

Hi felgall

I tried that code in apage that was workinf fine with the expanded script, and now it does not work

<script type="text/javascript">
 (function()  {
 var element = document.createElement("script");
     element.src = "Scripts/text.js";
     document.body.appendChild(element);
 }());
 </script>