Welcome to WebmasterWorld Guest from 3.80.4.76

Forum Moderators: open

Message Too Old, No Replies

Java Vars

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

New User

5+ Year Member

joined:Sept 6, 2011
posts: 29
votes: 0


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
12:31 am on Sept 7, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3147
votes: 3


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
1:47 am on Sept 7, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2011
posts: 29
votes: 0


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
7:23 am on Sept 7, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3147
votes: 3


... 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?
6:49 pm on Sept 7, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2011
posts: 29
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3147
votes: 3


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.
12:27 am on Sept 8, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2011
posts: 29
votes: 0


<script language="JavaScript" src="http://example.com/test.php"></script>
echos out how people are online with a link in it.
1:26 am on Sept 8, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3147
votes: 3


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.
2:29 pm on Sept 8, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:5021
votes: 26


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>
8:57 pm on Sept 8, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2011
posts: 29
votes: 0


Thanks Fotiman works great just like I wanted to thanks everyone for the help.
9:21 pm on Sept 8, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3147
votes: 3


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. :)
12:27 am on Sept 9, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 6, 2011
posts: 29
votes: 0


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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members