Forum Moderators: open

Message Too Old, No Replies

Java Vars

         

dcool86

9:58 pm on Sep 6, 2011 (gmt 0)

10+ Year Member



I been using php for awhile but still new at javascript. Im trying to pass some info example below

<script language="JavaScript">
var ref = (''+document.referrer+'');
</script>

<script language="JavaScript" src="http://example.com/test.php?r=ref"></script>

I need the script at the top to get the referring website the pass the info to test.php?r=Here

I cannot echo it in php as the sites the script goes on dosnt support it.

Thanks

penders

12:31 am on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Does the top script need to be separate? Or can it all be done in one go and write the document.referrer directly into the src attribute?

I'm assuming this script element should go in the HEAD section?

You might be able to get away with using document.write() to literally write out your script element as the document is created(?!), however, this uses the DOM...

Include this somewhere on the page:
function createScriptElement() { 
var elem = document.createElement('script');
elem.type = 'text/javascript';
elem.src = 'http://example.com/test.php?r=' + encodeURIComponent(document.referrer);
document.getElementsByTagName('head')[0].appendChild(elem);
}
createScriptElement();


Note that the URL parameter value needs to be escaped, much like urlencode() in PHP.

(Btw, the language attribute is deprecated, use type="text/javascript" instead, although even this is optional with HTML 5)

...welcome to WebmasterWorld.com

dcool86

1:47 am on Sep 7, 2011 (gmt 0)

10+ Year Member



It has to be separate as the bottom script prints document.write() the whole script has been written that way I just want to add a way to send the referrer to test.php?r=Here

Thanks

penders

7:23 am on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... the bottom script prints document.write() ...


Is the script element whose src attrib you want to change already being written to the page with document.write()? What control do you have over this script element? Where is this script element?

dcool86

6:49 pm on Sep 7, 2011 (gmt 0)

10+ Year Member



I cannot change the bottom script I can only add this is there a way or not?

Thanks

penders

8:42 pm on Sep 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You might be able to do this but may be not the way you are expecting. You don't give enough detail in order to give a sufficient answer.

In order to get the value of ref at the end of the src URL (with JavaScript) it needs to be written to the page. If the entire script element already exists then you can't simply use document.write(). However, if you have control over this script element then you perhaps could.

If the script element already exists without the 'r' url param as the page loads then it is going to make a request for this document before you've had a chance to append the ref value - this may not work for you?

Ideally, either you need to have control over what is written to the page (the document.write() I assume?), or the entire script element should not appear at all when the document is created.

What exactly is written out with document.write() in the 'bottom script'...? Does it include something like....

<script language="JavaScript" src="http://example.com/test.php"></script>


If this can not be altered before it is written to the page then you cannot prevent the request for test.php without the 'r' url param. However, you could discard this initial request (if you have control over test.php) and use the code from my first answer to add the 'r' param to the request via the DOM. A second request is then made for test.php, this time with the 'r' parameter. But I'm making assumptions about how your scripts are hanging together.

dcool86

12:27 am on Sep 8, 2011 (gmt 0)

10+ Year Member



<script language="JavaScript" src="http://example.com/test.php"></script>
echos out how people are online with a link in it.

penders

1:26 am on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If that script element is hard coded in your page and cannot be altered during page generation, as I think you are suggesting, and the output of that script writes directly to the page in-place then I'm afraid I don't think it is possible.

Even if we could later change the src attribute with script to append the URL parm, it won't work if the script is using document.write() to 'echo' content.

Fotiman

2:29 pm on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not a fan of document.write, but if you must then just replace this:

<script language="JavaScript" src="http://example.com/test.php?r=ref"></script>

with this:

<script>
document.write('<script src="http://example.com/test.php?r=' + ref + '"><\/script>');
</script>

dcool86

8:57 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



Thanks Fotiman works great just like I wanted to thanks everyone for the help.

penders

9:21 pm on Sep 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ha, I think I got the wrong end of the stick! Apologies. I was somehow thinking the actual script element itself was fixed. Glad you got it sorted. :)

dcool86

12:27 am on Sep 9, 2011 (gmt 0)

10+ Year Member



It's cool penders I wish I could of explained my self better but my lack of java skills isn't that great thanks though.