Reputation: 645
I have a javascript variable that contains an url, populated by outputting a script tag from php. The intention is to change the webpage to that url under certain circumstances.
var url = "/somepage?q=3®ion=1";
The problem is that the url contains the sequence "®" which internet explorer changes to "®" without being asked.
I've tried escaping the whole url with htmlspecialchars, which breaks other browsers. Turning off quirks mode might help, but it's not an option with the current system. Adding to the script tag did nothing.
edit: I figured out one solution, "escaping" the url with concatenation.
var url = '<?php implode( "&'+'", explode( '&', '/somepage?q=3®ion=1' ) ); ?>';
I also realized I should probably mention how I change the page:
document.location.href = url;
edit: Turns out I had an error in my script tag. A minimal example that shows the same "problem":
<script type="text/javascript" />
var url = "/test?mode=preview®ion=1";
</script>
The self-closing start tag is the important thing here.
Upvotes: 1
Views: 531
Reputation: 6159
It should be var url = "/somepage?q=3&region=1";
Always use &
in URLs so the browser understands this a query separator and doesn't think it refers to some htmlentity (the problem here is IE doesn't care if the semicolon is present or not and assumes ®
should be ®
)
EDIT: the real context was added after I answered and as pointed out in the question itself and in comments, you shouldn't use &
if you intend to use the variable to redirect using some window.location
BUT, since we have PHP at hand, I would suggest using some PHP code like this if the javascript redirect is not part of some other complex script:
<?php
header('Location: http://domain/somepage?q=3®ion=1');
exit();
?>
Upvotes: 4