2

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
asked Sep 20, 2012 at 18:09
1
  • 2
    That's not a valid URL. Anything after the first ? should be URI-encoded. Commented Sep 20, 2012 at 18:11

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

@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?
0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.