最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - & amp in url request - Stack Overflow

programmeradmin8浏览0评论

I'm getting values from the database and sending them trough url as request and it request is successful when there is no & in the value from the database now I assume that my request should look like value=something&value2=something with amp &

How can I replace all & occurrances with & in the url directly, is it possible to write javascript directly in the request?

I'm getting values from the database and sending them trough url as request and it request is successful when there is no & in the value from the database now I assume that my request should look like value=something&value2=something with amp &

How can I replace all & occurrances with & in the url directly, is it possible to write javascript directly in the request?

Share Improve this question edited Aug 10, 2010 at 15:40 Andy E 345k86 gold badges481 silver badges451 bronze badges asked Aug 10, 2010 at 15:31 LondonLondon 15.3k35 gold badges112 silver badges148 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

This is almost certain to spark a debate!

My understanding of your question is either of these two:

How do I pass data in the query string when my data contains an ampersand (&).

And the correct answer is that you escape it...

value=something%26with%26ampersands&value2=hello

You also need to escape lots of other characters too.

How do I write a link on a web page when ampersands (&) make my markup invalid

Technically you should write your links like this...

<a href="mypage?value=hello&amp;value2=world">Hello World</a>

This prevents your markup from getting "entity confusion" as the & character denotes an entity.

Both Together

<a href="mypage?value=something%26with%26ampersands&amp;value2=world">Hello World</a>

Can you escape an address using JavaScript

You can use:

var myAddress = escape("value=something&value2=something");

But it probably doesn't do what you want. It will encode special characters - but not * @ - _ + . /

This means you would get all & replaced with %26...

Server side languages do a much better job of this - what language are you using?

You can use the function encodeURIComponent:

var q = encodeURIComponent("value=something&value2=something with amp &amp;");

And you will get:

"value%3Dsomething%26value2%3Dsomething%20with%20amp%20%26amp%3B"

This sounds like a problem to be tackled right when you're building that URL, so server-side technologies are the issue, not client-side technologies like Javascript (unless you're using node.js or the like).

You should probably be using URL encoding instead when making that request, instead of HTML encoding. If you're using PHP to read from the database, the urlencode function will do the trick.

<?php
    echo urlencode('foo&bar'); // foo%26bar
?>

If I am understanding this correctly what you are looking to do is not to change & to &amp you are looking to change it to %26 which is the url escaped version. Try running your data through escape([string parameter]) before submitting it.

发布评论

评论列表(0)

  1. 暂无评论