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?
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&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&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 &");
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 &
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.
精彩评论