0

I want to add a web traffic analysis tool to my site.

To call it, I have to place the following in each page I want to track in my site:

<script type="text/javascript" src="http://analysisTool.js?idtag=123456789&orderid=random"></script>

The idtag 123456789 is the tag id assigned to my site

And orderid will be a random number to each page I want to track (it is the way the tool works)

As I don't want to edit the pages if I ever need to change this orderid, I have created myjs.js with:

var uid = Math.floor(Math.random()*(100000000-0))+0;

And then, I am doing:

<script type="text/javascript" src="/_includes/myjs.js"></script>
<script type="text/javascript" src="http://analysisTool.js?idtag=123456789& orderid ="+uid></script>

But this must be a wrong way to do it because in the browser tools, in the net chapter, I cannot see the orderid.

I’ve checked the path to myjs.js is correct.

What am I doing wrong?

Thanks!

4 Answers 4

1

You have to add the script-tag completely with javascript. You cant use javascript variables like that in the HTML page.

If you want to add a script-tag dynamicly use this script:

var script = document.createElement('script'); //create new tag
script.src = "http://analysisTool.js?idtag=123456789&orderid=" + uid;//set the url with your id
document.getElementsByTagName('head')[0].appendChild(script);//append the scripttag
Sign up to request clarification or add additional context in comments.

Comments

0

Please take a look at jQuery getScript().

I think it could solve your problem:

By default, $.getScript() sets the cache setting to false. This appends a >timestamped query parameter to the request URL to ensure that the browser >downloads the script each time it is requested.

And please note that "document.write" also works in this case.

Comments

0

You were trying to access uid in a wrong way (in the src attribute). It needs to be in a script context so your only option is to add the script dinamically:

<script type="text/javascript" src="/_includes/myjs.js"></script>
<script type="text/javascript">
    var fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript")
    fileref.setAttribute("src", "http://analysisTool.js?idtag=123456789& orderid ="+uid);
    document.getElementsByTagName("head")[0].appendChild(fileref)
</script>

Comments

0

please add this js declaration dynamic in your myjs.js. Means create the script tag through your javascript code.

window.onload = function() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    var uid = Math.floor(Math.random()*(100000000-0))+0;
    script.src = 'http://analysisTool.js?idtag=123456789&orderid=' + uid;
    document.head.appendChild(script);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.