0

I need to create a form which opens a url in view-source format

I've tried adding "view-source" to the action, which opens the new url in view-source mode, but without adding the form params in the url

<form action="view-source:http://yahoo.com" target="_blank">
  Key: <input type="TEXT" name="key" required>
</form>

the above will open yahoo.com in view-source, but without the ?key=abc which is entered in the form

if I remove the view-source from the action, it will add the parameter but then I will have to manually click view-source

I am working with chrome, not sure how other browsers will act

Thoughts anyone? Tx Tomer

Tomer
  • 859
  • 3
  • 11
  • 19

2 Answers2

0

If you're willing to add some JavaScript:

document.getElementById( 'view-source-form' ).onsubmit = function() {
    this.action += "?key=" + this.key.value;
};

This changes the form's action URL to include the parameter. You also need to add id="view-source-form" to the form tag or use some other way to target the form.

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • I thought about that but I have multiple parameters in the form and it is updated often, So I would prefer a way which will be more maintaniable – Tomer Nov 10 '13 at 12:33
  • You can build the query string dynamically. Most JS frameworks provide a function for that, and here's a vanilla JavaScript solution: http://stackoverflow.com/a/5340658/502381 – JJJ Nov 10 '13 at 12:51
0

eventually I found the following solution using jquery

$("#form").submit(function(event) {
    $("#form").attr("action", "view-source:" + $("#form").attr("action") + "?" + $("#form").serialize());
});

which seems to do the trick nicely, after adding id="form" to the FORM tag

Tomer
  • 859
  • 3
  • 11
  • 19