Welcome to WebmasterWorld Guest from 18.208.159.25

Forum Moderators: open

Message Too Old, No Replies

Google Adwords Dynamic Keyword Insertion

How to change %20 to the Plus Sign?

     
10:12 am on Jun 26, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:May 5, 2004
posts:59
votes: 0


Hello,

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..!

12:41 pm on June 26, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


A hearty welcome to to WebmasterWorld, emoshe.

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)
2:03 pm on June 26, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:May 5, 2004
posts:59
votes: 0


Dear Bernard,

Thank you so much for your prompt reply & the warm greeting. Your solution works flawlessly! I really appreciate it.

Take care,
Eyal