Forum Moderators: open

Message Too Old, No Replies

How to change HTTP_REFERER "&" to a "+"

         

abstravel

5:40 pm on Mar 22, 2006 (gmt 0)

10+ Year Member


Would anyone be able to asist me with the correct javascript to change the value of the HTTP_REFERER (&) to a plus (+) sign?

I need to change the (&) html to a plus sign (+) so as to escape any parsing problems.

Thanks,

Douglas

DrDoc

7:24 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld! [WebmasterWorld.com]

What I would probably do is use the replace() function and a simple regular expression.

orig = document.referrer;
chgd = orig.replace(/&/, '+');

abstravel

8:26 pm on Mar 22, 2006 (gmt 0)

10+ Year Member


DrDoc:

Thanks. I tried the following but I'm at a loss as to the correct javascript.
I'm trying to send the HTTP_REFERER via a form as noted below.
The script that handles the form can not parse (&), thus my need to change those to a (+) plus sign.
Below is a test of what I'm accessing, but I still get an error, don't know why..
-------------------------------------------
<head>
<script type="text/javascript">
function replace()
{orig = document.referrer;
chgd = orig.replace(/&amp;/, '+');
}
<script>
</head>

<body>
<form>.....blah, blah
<input type=hidden name="SO_GL" value='<?xml version="1.0" encoding="iso-8859-1"?><SO_GL><GLOBAL_LIST><NAME>SITE_LIST_EXTERNAL_REMARK</NAME><LIST_ELEMENT><CODE>HTTP REFERER</CODE><LIST_VALUE>

<script><document.write("referrer()")></script>

</LIST_VALUE></LIST_ELEMENT></GLOBAL_LIST></SO_GL>'>
<input type="submit" value="Submit"></FORM>

</body>
--------------------------

Regards,

Douglas

whoisgregg

9:06 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm pretty sure you can't do document.write inside of a value="", but I'm not sure.

Try something like this...

<head>
<script type="text/javascript">
<!--
function replace()
{
var orig = document.referrer;
var chgd = orig.replace(/&amp;/, '+');
var completestring = '<?xml version="1.0" encoding="iso-8859-1"?>' + '<SO_GL><GLOBAL_LIST><NAME>SITE_LIST_EXTERNAL_REMARK<\/NAME>' + '<LIST_ELEMENT><CODE>HTTP REFERER<\/CODE>' + '<LIST_VALUE>'+chgd+'<\/LIST_VALUE>' + '<\/LIST_ELEMENT><\/GLOBAL_LIST><\/SO_GL>';
var the_xml_input_obj = document.getElementById('the_xml_input');
the_xml_input_obj.value = completestring;
alert(the_xml_input_obj.value);
}
-->
</script>
</head>
<body>
<form>.....blah, blah
<input type=hidden name="SO_GL" value='' id="the_xml_input">
<script type="text/javascript">replace();</script>
<input type="submit" value="Submit"></FORM>
</body>

It loads the xml string into a variable and concatenates in the referrer value. Then pops it into the value of the input.

This would probably be better as a server side thing though. Do you have access to PHP or ASP? I can provide sample code for PHP.

<input type=hidden name="SO_GL" value='<?xml version="1.0" encoding="iso-8859-1"?>
<SO_GL><GLOBAL_LIST><NAME>SITE_LIST_EXTERNAL_REMARK</NAME>
<LIST_ELEMENT><CODE>HTTP REFERER</CODE>
<LIST_VALUE>
<?php echo $_SERVER['HTTP_REFERER'];?>
</LIST_VALUE>
</LIST_ELEMENT></GLOBAL_LIST></SO_GL>'>
<input type="submit" value="Submit"></FORM>

DrDoc

9:20 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... don't give your JavaScript function a function name ;) Rename "function replace()" something else, like "function removeAmp()"

whoisgregg

9:29 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Doh! I should have caught that and changed the name. :/

Also, be sure to take out the "alert(the_xml_input_obj.value); " I put in there for debugging.

Bernard Marx

10:03 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is nothing wrong in using
replace
as a function name. There is no global method or property called
replace
, only
location
and
String.prototype
methods.

DrDoc

10:09 pm on Mar 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nothing wrong, perhaps, but a good habit of avoiding

abstravel

10:17 pm on Mar 22, 2006 (gmt 0)

10+ Year Member



Thanks everyone.

It works on the browser, but does NOT send the information in the form value.
Still trying to debug that part.
I'm going to try to call it from a .js file and see if it works with an onload function.

Regards,

Douglas

abstravel

11:57 pm on Mar 22, 2006 (gmt 0)

10+ Year Member


Still dumbfounded trying to get this script to work in a .js file and sending the value in a form.

This is what I have so far
-------- form.js ------------------
function replace(){
var orig = document.referrer;
var chgd = orig.replace(/&amp;/g, +); //replace the &amp; sign to a + sign
var chgd = orig.replace(/&/g, +); //replace any & sign to a + sign
document.myform.SO_GL.value = "<?xml version="1.0" encoding="iso-8859-1"?><SO_GL><GLOBAL_LIST><NAME>SITE_LIST_EXTERNAL_REMARK</
NAME><LIST_ELEMENT><CODE>HTTP REFERER</CODE><LIST_ELEMENT><CODE>UD1</
CODE><LIST_VALUE><!--#echo var="server_addr"--></LIST_VALUE></
LIST_ELEMENT><LIST_ELEMENT><CODE>UD2</CODE><LIST_VALUE><!--#echo var="
script_filename" --><!--#echo var="QUERY_STRING" --></LIST_VALUE></
LIST_ELEMENT><LIST_ELEMENT><CODE>UD3</CODE><LIST_VALUE>USERIP: <!--#echo var
="remote_addr" --></LIST_VALUE></LIST_ELEMENT><LIST_ELEMENT><CODE>UD4</
CODE><LIST_VALUE>USERHOST: <!--#echo var="REMOTE_HOST" --></LIST_VALUE></
LIST_ELEMENT><LIST_ELEMENT><CODE>UD5</CODE><LIST_VALUE>+chgd+</LIST_VALUE></
LIST_ELEMENT></GLOBAL_LIST></SO_GL>"
}

function formValidator (theForm) {
replace();
}
-----------------------------------

On the web page I call it via:
<head>
<script language="JavaScript" src="http:/[smilestopper]/www.example.com/form.js" type="text/javascript"></script>
</head>

Then from the <body>

<Form name="myform" method="POST" onSubmit="return formValidator(this)">

<input type=hidden name="SO_GL" value="">

abstravel

4:57 am on Mar 23, 2006 (gmt 0)

10+ Year Member


Just found that the script works when doing an alert. Apparently the new window catches the script output.
But, when checking the source code, the script inside the page does nothing.
Also, when parsing the hidden input value in the form, it gets sent with the raw html "+chgd+", like if it was just a regular document.write of the script above.

Any help is welcomed, I don't know how to make it work, but I'm sure there is a way to parse information into the form.

Thanks,

Douglas
------------------------------

<HEAD>
<script type="text/javascript">
<!--
function replace()
{
var orig = document.referrer;
var chgd = orig.replace(/&amp;/, '+');
var chgd = orig.replace(/&amp;/, '+');
var completestring = '<?xml version="1.0" encoding="iso-8859-1"?
><SO_GL><GLOBAL_LIST><NAME>SITE_LIST_EXTERNAL_REMARK<\/
NAME><LIST_ELEMENT><CODE>http_referrer<\
/CODE><LIST_VALUE>'+chgd+'<\/LIST_VALUE><\/LIST_ELEMENT><\/GLOBAL_LIST><\/
SO_GL>';
var the_xml_input_obj = document.getElementById('the_xml_input');
document.abstravel.SO_GL.value = completestring;
}
window.onload = replace;
-->
</head>

<body><form...etc>

</script>
<input type=hidden name="SO_GL" id='the_xml_input' value="<script type="text
/javascript">replace();</script>">
</form></body...etc>