Welcome to WebmasterWorld Guest from 54.221.9.6

Forum Moderators: open

Message Too Old, No Replies

Javascript doesn't like spaces.

.. being passed in a URL

     
1:36 am on Jul 30, 2006 (gmt 0)

New User

10+ Year Member

joined:Apr 5, 2006
posts:18
votes: 0


Hullo all. Bit of a Javascript problem here; I'm not very familiar with it so tend to try and attack someone else's script until it's how I need it. Here's my code:

<script language="Javascript1.2">document.write('<a href="javascript:void(0)" onclick=window.open("/js/recommend.php?loc=http://www.site.net/news/0001&story=Large elephant loose","","height=280,width=260,top=150,left=200,toolbar=no,menubar=no,location=no,status=no,resizable=1"); title="Recommend this to a friend">Send to a Friend (testing)</a>');</script>

What this is meant to be is a simple "send to a friend" script. This code generates a link to the script (which works just fine). I'm using variables in the address-line to be used later - that's the 'loc' there, and the 'story'.

The problem is that Javascript doesn't seem to like those spaces in the 'story' variable. Php doesn't care, but Javascript does. Is there any reason for this or a way around it? Thanks.

1:45 am on July 30, 2006 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 31, 2003
posts:9074
votes: 6


You should always encode the spaces in the URL - this will ensure much better compatibility in browsers and will almost certainly fix your JS problem. You should also be encoding the ampersand too within the link:

&[b]amp;[/b]story=Large[b]%20[/b]elephant[b]%20[/b]loose
3:31 am on July 30, 2006 (gmt 0)

New User

10+ Year Member

joined:Apr 5, 2006
posts:18
votes: 0


The trouble is that I'm dynamically pulling the "story" part from elsewhere - it's the associated headline, which contains spaces. I'm just trying to pass it to another page, so have done it in the URL to GET later.
9:02 am on July 30, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member kaled is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 2, 2003
posts:3710
votes: 0


Why are you using location=no,status=no etc. - this is guaranteed to annoy people greatly.

What purpose does void(0); serve?

If all you want to do is open a new window, use the target attribute with the link and use the js function escape to perform the encoding.

If the url is known when the page is served, surely the link should be created in php without any need for javascript.

Kaled.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members