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?
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>
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;
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.
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?
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.
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?
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?