Forum Moderators: open
As I don't really know JS, I will be really happy if someone can help me with this:
I have a JavaScript that takes a variable from the URL string and put it into the HTML page. Here is the code:
==== START OF CODE ====
<script language="JavaScript">
<!--
// Get the value of the variable "key" and print
if (document.URL.indexOf('?key=')!= -1)
addr_str=document.URL.substring(document.URL.indexOf('?key=')+5, document.URL.length);
else if (document.URL.indexOf('&key=')!= -1)
addr_str=document.URL.substring(document.URL.indexOf('&key=')+5, document.URL.length);
else
{
var addr_str = 'Default String';
}
document.write(addr_str.replace(/\+/gi, ' '));
-->
</script>
==== END OF CODE ====
The problem is that when I am using Google Adwords Keyword Insertion - {KeyWord}, and the phrase is more than one word, the final result I have in the page is like:
FirstKeyword%20SecondKeyword%20ThirdKeyword...
So I simply need to change the %20 to spaces (or to the '+' sign which will be translated to spaces).
I will be very happy if someone can help me with editing this script so it will work with the keyword insertion...
Thank you in advance..!
Try this:
(Note that you must replace the broken pipe character ¦ for a real one)
// Get the value of the variable "key"
var addr_str = "Default String";
var matches = location.search.match(/(\?[b][red]¦[/red][/b]&)key=([^&]*)/);
// Check that match not null, and value non-empty
// Then resolve encoded spaces
if(matches && matches[2])
addr_str = matches[2].replace(/%20/g,"+");
alert(addr_str)