Search code examples
javascriptjqueryhtmlgreasemonkey

jQuery created button dropping link text


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?


Solution

  • 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