1

I have created a dyanmic button in Java script with the following code:

var pathname = window.location.pathname;    
var html = '<div id="gmSomeID"><form action="https://apples.com/active/index?u='+pathname+'"><input id="tapButton" type="submit" value="TAP" /></form></div>'

That should pass direct someone to the link mentioned above with pathname appended.

When I paste this URL in myself manually this works. However, each time I click this link, the URL is truncated to just:

https://apples.com/active/index?

For reference:

Why would JavaScript / Browser be truncating a link like this?

NickP
  • 1,354
  • 1
  • 21
  • 51

2 Answers2

1

Not sure if this is causing your problem, but every time you construct parameters of a URL, you should URL-encode the values.

So, to be correct, the code should look like this:

var pathname = window.location.pathname;    
var html = '<div id="gmSomeID"><form action="https://apples.com/active/index?u='+encodeURIComponent(pathname)+'"><input id="tapButton" type="submit" value="TAP" /></form></div>'
Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
  • Not sure if this would work out for a URL that is SEO Friendly: `https://apples.com/class/1/info`... – Praveen Kumar Purushothaman Jul 13 '17 at 13:09
  • Thanks for the heads up. Though, unfortunately this seems not to solve the problem. – NickP Jul 13 '17 at 13:12
  • @NickP: I suspect your problem is somewhere else than the browser/Javascript, or in some other piece of code. I tried your code with your inputs and it works as expected. Maybe there's some URL rewriting being done on the server (or some redirects)? Maybe some other piece of JS on the page is affecting how forms are submitted? – Tom Pažourek Jul 13 '17 at 13:16
  • Thanks Tom. Its strange as when I paste https://apples.com/active/index?u=/class/1/info into the browser directly, this seems to load the correct page so agree must be something else with the code. – NickP Jul 13 '17 at 13:17
1

I thought about this, and had another idea:

Try putting the u parameter as a hidden input instead of action.

var html = '<div id="gmSomeID"><form action="https://apples.com/active/index"><input type="hidden" name="u" value="'+pathname+'"/><input id="tapButton" type="submit" value="TAP" /></form></div>'

I think that GET input values always override the values in the query string of the action parameter.

See also: submitting a GET form with query string params and hidden params disappear

Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107