I want this entire string http://mywebsite.com?u=http://othersite.com?thisis sent at once . if I put it in a URL shortener like bit.ly it works, but not if I leave it as it is it breaks.
<script>
function go(){
window.frames[0].document.body.innerHTML='<form target="_parent" action="http://mywebsite.com?u=http://othersite.com?thisis"></form>';
window.frames[0].document.forms[0].submit()
}
</script>
kapa
78.9k21 gold badges166 silver badges178 bronze badges
-
2That's not a valid URL. Anything after the first ? should be URI-encoded.Blazemonger– Blazemonger2012年09月20日 18:11:21 +00:00Commented Sep 20, 2012 at 18:11
2 Answers 2
You have to escape the nested URL with encodeURIComponent() in order to make the URL valid.
That means doing something like this.
function go(){
var uri = 'http://mywebsite.com?u='
+ encodeURIComponent('http://othersite.com?thisis');
window.frames[0].document.body.innerHTML =
'<form target="_parent" action="'
+ uri
+ '"></form>';
window.frames[0].document.forms[0].submit();
}
answered Sep 20, 2012 at 18:11
kapa
78.9k21 gold badges166 silver badges178 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
kapa
@RinaLegrande It works fine. Strange, because the answer you have accepted has the very same code, without using variables for readability (that does not change anything). Could you explain what is not working?
try
<script>
function go(){
window.frames[0].document.body.innerHTML='<form target="_parent" action="http://mywebsite.com?u=' + encodeURIComponent('http://othersite.com?thisis') + '"></form>';
window.frames[0].document.forms[0].submit()
}
</script>
answered Sep 20, 2012 at 18:13
sroes
15.1k1 gold badge58 silver badges75 bronze badges
Comments
lang-js